Day10 综合任务1

1、 continue 是进入下一次循环,而 break 是跳出整个循环体;

2、 tempRandom.nextInt(n) 就可以生成0-n之间的随机数,

3、用循环每次求学生的总成绩,如果有不及格的成绩就不再参与评比,每次成绩对比后留下最高成绩和这个学生的编号,

 1 import java.util.Arrays;
 2 import java.util.Random;
 3 
 4 public class Task1 {
 5     public static void main(String args[]) {
 6         task1();
 7     }// Of main
 8 
 9     public static void task1() {
10         // Step 1. Generate the data whie n students and m sourses.
11         // Set these values bug yourself.
12         int n = 10;
13         int m = 3;
14         int lowerBound = 50;
15         int upperBound = 90;
16         int threshold = 60;
17 
18         // Here we have to use an object to generate random numbers.
19         Random tempRandom = new Random();
20         int[][] data = new int[n][m];
21         for (int i = 0; i < n; i++) {
22             for (int j = 0; j < m; j++) {
23                 data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);
24             } // Of for j
25         } // Of for i
26 
27         System.out.println("The data is:\r\n" + Arrays.deepToString(data));
28 
29         // Step 2. Compute the total score of each student.
30         int[] totalScores = new int[n];
31         for (int i = 0; i < n; i++) {
32             for (int j = 0; j < m; j++) {
33                 if (data[i][j] < threshold) {
34                     totalScores[i] = 0;
35                     break;
36                 } // Of if
37 
38                 totalScores[i] += data[i][j];
39             } // Of for j
40         } // Of for i
41         System.out.println("The total scores are:\r\n" + Arrays.toString(totalScores));
42 
43         // Step 3. Find the best and worst student.
44         // Typical initialization for index: invalid value.
45         int tempBestIndex = -1;
46         int tempWorstIndex = -1;
47         // Typical initialization for best and worst values.
48         int tempBestScore = 0;
49         int tempWorstScore = m * upperBound + 1;
50         for (int i = 0; i < n; i++) {
51             // Do not consider failed students.
52             if (totalScores[i] == 0) {
53                 continue;
54             } // Of if
55 
56             if (tempBestScore < totalScores[i]) {
57                 tempBestScore = totalScores[i];
58                 tempBestIndex = i;
59             } // Of if
60 
61             if (tempWorstScore > totalScores[i]) {
62                 tempWorstScore = totalScores[i];
63                 tempWorstIndex = i;
64             } // Of if
65         } // Of for i
66 
67         // using "else if",because a student can be both wht best and the worst
68         // Step 4. Output the student number and score.
69         if (tempBestIndex == -1) {
70             System.out.println("Cannot find the best student. All sudent have failed.");
71         } else {
72             System.out.println("The best student is No." + tempBestIndex + " with scores: "
73                     + Arrays.toString(data[tempBestIndex]));
74         } // Of if
75 
76         if (tempWorstIndex == -1) {
77             System.out.println("Cannot find the best student. All sudent have failed.");
78         } else {
79             System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
80                     + Arrays.toString(data[tempWorstIndex]));
81         } // Of if
82 
83     }// Of task1
84 }

 4、关于 Arrays.toString() 和 Arrays.deepToString() , deepToString 是输出多维数组的, toString 是输出一维的,这里的一维是相对的。比如这里 tem 虽然是二阶数组,但是 tem[1] 就是指 {3, 4} ,也是一维的。 tem.toString() 就是输出地址。

 

 1 import java.util.Arrays;
 2 public class toString {
 3     public static void main(String args[]) {
 4         int[][] tem = new int[][]{
 5             {1,2},
 6             {3,4},
 7             {5,6}
 8         };
 9         System.out.println(Arrays.deepToString(tem));
10         System.out.println(Arrays.toString(tem[1]));
11         System.out.println(tem.toString());
12         System.out.println(tem[1].toString());
13 
14     }
15     
16 }

 

posted @ 2022-04-26 20:06  滑稽的炒饭  阅读(22)  评论(0)    收藏  举报