4.16上机练习

1.有10个评委打分,(去掉一个最高一个最低)求平均分。

import java.nio.channels.ScatteringByteChannel;
import java.util.Scanner;
public class a11 {
    public static void main(String[] args){
        // TODO Auto-generated method stud
        int[] a = new int[]{12, 18, 20, 11, 14};
        int t;
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j + 1] > a[j]) {
                    t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
        double sum=0;
        for (int k = 1; k <a.length-1 ; k++) {
            sum+=a[k];
        }
        System.out.println("平均分"+sum/(a.length-2));
}
}

2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。

import java.util.Random;
import java.util.Scanner;
public class a11 {
    public static void main(String[] args){
        // TODO Auto-generated method stud
         int[] a = new int[10];
            Random r=new Random();
            for (int i = 0; i < a.length; i++) {
                a[i]=r.nextInt(100);
            }
            int t;
            for (int i = 0; i < a.length - 1; i++) {
                for (int j = 0; j < a.length-1-i; j++) {
                    if (a[j + 1] > a[j]) {
                        t = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = t;
                    }
                }
            }
            for (int i : a) {
                System.out.println(i);
            }
}
}

3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)

import java.util.Random;
import java.util.Scanner;
public class a11 {
    public static void main(String[] args){
        // TODO Auto-generated method stud
        int[] score = new int[7];
        Random r = new Random();
        for (int i = 0; i < score.length; i++) {
            score[i] = r.nextInt(35) + 1;
        }
        for (int i = 0; i < score.length; i++) {
            System.out.print(score[i]);
            System.out.print(" ");
        }
}
}

4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数

import java.util.Random;
import java.util.Scanner;
public class a11 {
    public static void main(String[] args){
        // TODO Auto-generated method stud
        int[] a = new int[]{14,23,21,3,69,85,46,33,53,27};
        int max=a[0];
        int mini=a[0];
        int jishu=0;
        int oushu=0;
        for (int i = 1; i <a.length ; i++) {
            if (a[i]>max){
                max=a[i];
            }
        }
        for (int j = 1; j <a.length ; j++) {
            if (a[j]<mini){
                mini=a[j];
            }
        }
        for (int k = 0; k< a.length; k++) {
            if (a[k]%2==0){
                oushu+=1;
            }else {
                jishu+=1;
            }
        }
        System.out.println("最大值为"+max+"最小值为"+mini+"偶数有"+oushu+"个"+"奇数有"+jishu+"个");
}
}

 

posted @ 2020-04-16 12:27  momizi  阅读(93)  评论(0编辑  收藏  举报