第七次上机练习

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

 1 package one;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test01 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         int[] score = new int[10];
10         Scanner s = new Scanner(System.in);
11         System.out.println("请输入10位评委的分数");
12         for (int i = 0; i < score.length; i++) {
13             score[i] = s.nextInt();
14         }
15         for (int i = 0; i < score.length - 1; i++) {
16             for (int j = 0; j < score.length - 1 - i; j++) {
17                 if (score[j] > score[j + 1]) {
18                     int tmp = score[j];
19                     score[j] = score[j + 1];
20                     score[j + 1] = tmp;
21                 }
22             }
23         }
24         int sum = 0;
25         for (int i = 1; i < score.length - 1; i++) {
26             sum += score[i];
27         }
28         System.out.println("平均分为:" + sum / (score.length - 2.0));
29     }

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

 1 package one;
 2 
 3 import java.util.Random;
 4 
 5 public class Test01 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         int[] score = new int[10];
10         Random r = new Random();
11         for (int i = 0; i < score.length; i++) {
12             score[i] = r.nextInt(100);
13         }
14         for (int i = 0; i < score.length - 1; i++) {
15             for (int j = 0; j < score.length - 1 - i; j++) {
16                 if (score[j] > score[j + 1]) {
17                     int tmp = score[j];
18                     score[j] = score[j + 1];
19                     score[j + 1] = tmp;
20                 }
21             }
22         }
23         for (int i = 0; i < score.length; i++) {
24             System.out.println(score[i]);
25         }
26     }
27 
28 }

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

 1 package one;
 2 
 3 import java.util.Random;
 4 
 5 public class Test01 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         int[] score = new int[7];
10         Random r = new Random();
11         for (int i = 0; i < score.length; i++) {
12             score[i] = r.nextInt(35) + 1;
13         }
14         System.out.println("35选7号码是:");
15         for (int i = 0; i < score.length; i++) {
16             System.out.println(score[i]);
17         }
18     }
19 
20 }

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

 1 package one;
 2 
 3 import java.util.Random;
 4 
 5 public class Test01 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         int[] score = new int[10];
10         Random r = new Random();
11         for (int i = 0; i < score.length; i++) {
12             score[i] = r.nextInt(100);
13         }
14         System.out.println("原数组为:");
15         for (int i = 0; i < score.length; i++) {
16             System.out.println(score[i]);
17         }
18         for (int i = 0; i < score.length - 1; i++) {
19             for (int j = 0; j < score.length - 1 - i; j++) {
20                 if (score[j] > score[j + 1]) {
21                     int tmp = score[j];
22                     score[j] = score[j + 1];
23                     score[j + 1] = tmp;
24                 }
25             }
26         }
27         int ji = 0, ou = 0;
28         for (int i = 0; i < score.length; i++) {
29             if (score[i] % 2 == 0) {
30                 ou++;
31             } else {
32                 ji++;
33             }
34         }
35         System.out.println("最小值为:" + score[0]);
36         System.out.println("最大值为:" + score[score.length - 1]);
37         System.out.println("奇数个数:" + ji);
38         System.out.println("偶数个数:" + ou);
39     }
40 
41 }

 

posted @ 2020-04-16 13:37  宋金辉  阅读(120)  评论(0)    收藏  举报