第七次JAVA作业

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

package demo01;
import java.util.*;
public class demo001 {
        public static void main(String[] args) {
            Scanner input=new Scanner (System.in);
            int [] arr= new  int [10];
            int sum=0;
            System.out.println("请输入成绩");
            for (int i = 0; i < arr.length; i++) {
                System.out.print(i+1+":");
                arr[i]=input.nextInt();        
            }
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length-i-1; j++) {                             
                    if(arr[j]>arr[j+1]){
                        int h=arr[j];
                        arr[j]=arr[j+1];
                        arr[j+1]=h;
                    }
                }
            }                     
            for (int i = 1; i < 9; i++) {
                sum+=arr[i];                     
            }
                     
            System.out.println("选手分数平均值为"+sum/8.0);
        }
             
}      

 

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

package demo01;
import java.util.*;
public class demo001 {
    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);
        }
        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;
                }
            }
        }
        for (int i = 0; i < score.length; i++)
        {
            System.out.println(score[i]);
        }
    }
}

 

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

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

 

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

package demo01;
import java.util.*;
public class demo001 {
    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);
    }
}

 

posted @ 2020-04-16 14:38  三好学生·  阅读(184)  评论(0编辑  收藏  举报