java第七周上机作业

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

package asdf;

import java.util.Scanner;

public class cg {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] score = new int[10];
        Scanner s = new Scanner(System.in);
        System.out.println("请输入10位评委的分数");
        for (int i = 0; i < score.length; i++) {
            score[i] = s.nextInt();
        }
        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 sum = 0;
        for (int i = 1; i < score.length - 1; i++) {
            sum += score[i];
        }
        System.out.println("平均分为:" + sum / (score.length - 2.0));
    }

}

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

package asdf;

import java.util.Random;

public class cg {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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 asdf;

import java.util.Random;

public class cg {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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 asdf;

import java.util.Random;

public class cg {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        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 12:45  yjyweyyang  阅读(179)  评论(0编辑  收藏  举报