第七次上机练习

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

package yukikaze;
import java.util.Scanner;
public class mhm {
    public static void main(String[] args) {
        avg10();
        }
        public static void avg10() {
        int[] array = {89, 89, 92, 94, 78, 91, 88, 92, 90, 90};
        int index = 0, sum = 0, temp = 0, avg = 0 ;
        for (int i = 0; i < array.length - 1; i++) {
        for (int j = i + 1; j < array.length; j++) {
        if (array[j] < array[i]) {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        }
        }
        }
        while (array.length != index) {
        sum += array[index];
        index++;
        }
        do {
        sum = sum - array[0] - array[array.length-1]; //去掉最高分和最低分,
        } while (false);
        avg = sum / 8;
        System.out.println(avg);
        }
}

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

package yukikaze;
import java.util.Scanner;
import java.util.Arrays;
public class mhm {
    public static void main(String[] args) {
          // TODO Auto-generated method stub
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个范围");
        System.out.println("请输入最大值");
        int n = s.nextInt();
        System.out.println("请输入最小值");
        int m = s.nextInt();
        int a[] = new int[10];
        for (int t = 0; t < a.length; t++) {
            a[t] = m + (int) (Math.random() * n - m);
        }
        Arrays.sort(a);
        for (int y : a) {
            System.out.print(y+"\t");

        }
    }
}

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

package yukikaze;
import java.util.Random;
public class mhm {
    public static void main(String[] args)
    {
        Random r = new Random();
        int i=r.nextInt(35)+1;
        System.out.println("35选7号码为:");
        for (int j = 0; j <7; j++)
        {
            int k = r.nextInt(35)+1;
            System.out.println(j);
        }
    }
}

4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数

package yukikaze;
import java.util.Random;
public class mhm {
    public static void main(String[] args)
    {
        int[] score = new int[10];
        Random r = new Random();
        for (int i = 0; i < score.length; i++)
        {
            score[i] = r.nextInt(100);
        }
        System.out.println("原数组为:");
        for (int i = 0; i < score.length; i++)
        {
            System.out.println(score[i]);
        }
        for (int i = 0; i < score.length - 1; i++)
        {
            for (int j =0; j < score.length - 1 - i; j++)
            {
                if (score[j] > score[j + 1])
                {
                    int tmp = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = tmp;
                }
            }
        }
        int ji = 0, ou = 0;
        for (int i = 0; i < score.length; i++)
        {
            if (score[i] % 2 == 0)
            {
                ou++;
            }
            else
            {
                ji++;
            }
        }
        System.out.println("最小值为:" + score[0]);
        System.out.println("最大值为:" + score[score.length - 1]);
        System.out.println("奇数个数:" + ji);
        System.out.println("偶数个数:" + ou);
    }
}

猜你喜欢

转载自www.cnblogs.com/yukikazechino/p/12711699.html