1.综合任务一
任务内容:学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。任务要求:
1、进行学生成绩的随机生成, 区间为 [50, 100]。
2、找出成绩最好、最差的同学。但有挂科的同学不参加评比。
1.代码演示
package basic;
import java.util.Arrays;
import java.util.Random;
/**
* This is the tenth code, also the first task.
*
* @author WU JUN 2298320301@qq.com.
*/
public class Day10 {
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
task1();
}// Of main
/**
*********************
* Method unit test.
*********************
*/
public static void task1() {
// Step 1. Generate the data with n students and m courses.
// Set these values by yourself.
int n = 10;
int m = 3;
int lowerBound = 50;
int upperBound = 65; // Should be 100. I use this value for testing.
int threshold = 60;
// Here we have to use an object to generate random numbers.
Random tempRandom = new Random();
int[][] data = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
data[i][j] = lowerBound + tempRandom.nextInt(100 - 50);
} // Of for j
} // Of for i
System.out.println("The data is:\r\n" + Arrays.deepToString(data));
// Step 2. Compute the total score of each student.
int[] totalScores = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (data[i][j] < threshold) {
totalScores[i] = 0;
break;
} // Of if
totalScores[i] += data[i][j];
} // Of for j
} // Of for i
System.out.println("The total scores are:\r\n" + Arrays.toString(totalScores));
// Step 3. Find the best and worst student.
// Typical initialization for index: invalid value.
int tempBestIndex = -1;
int tempWorstIndex = -1;
// Typical initialization for best and worst values.
// They must be replaced by valid values.
int tempBestScore = 0;
int tempWorstScore = m * upperBound + 1;
for (int i = 0; i < n; i++) {
// Do not consider failed students.
if (totalScores[i] == 0) {
continue;
} // Of if
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
} // Of if
// Attention: This if statement cannot be combined with the last one
// using "else if", because a student can be both the best and the
// worst. I found this bug while setting upperBound = 65.
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
} // Of if
} // Of for i
// Step 4. Output the student number and score.
if (tempBestIndex == -1) {
System.out.println("Cannot find best student. All students have failed.");
} else {
System.out.println("The best student is No." + tempBestIndex + " with scores: "
+ Arrays.toString(data[tempBestIndex]));
} // Of if
if (tempWorstIndex == -1) {
System.out.println("Cannot find worst student. All students have failed.");
} else {
System.out.println("The worst student is No." + tempWorstIndex + " with scores: "
+ Arrays.toString(data[tempWorstIndex]));
} // Of if
}// Of task1
}// Of class Task1
1.2代码讲解
1、我们先从主函数入手展开分析,上述代码的主函数中只有一个task1方法,所以接下来我们跳转进入到task1方法进行分析。
2、首先task1方法不需要传入任何参数,并且也没有返回值。接下来对函数内部的内容进行分析,函数一开始就是定义初始化了一些列的局部变量(仅在该函数内起作用)。往下看我们会遇到这样段代码Random tempRandom = new Random()这条代码是用来创建一个对象用的,Random方法是Java自带的但是在用之前需要在代码顶部写入import java.util.Random语句。补充:Random返回值是一个伪随机选择的数。
继续往后就是两个for语句的嵌套循环,这个循环总体功能是用来将获取的随机数填入到我们的矩阵数组之中。其中我们需要注意的是data[i][j] = lowerBound + tempRandom.nextInt(100 - 50)语句,该语句的功能是使得学生成绩在50~100之间。tempRandom.nextInt(100 - 50)的功能是随机获取一个从[0~50)的整数。接下来是第二个嵌套for循环,这个for循环主要是用来计算每一个学生的总成绩,并将学生的总成绩存入到totalScores数组中。我们需要注意一下这个嵌套循环的内部有一个if判断语句,if语句的判断条件是当学生的某一科成绩小于60分时则总成绩记为零。在if语句中还有一个break语句我们来理解一些当执行break语句之后程序会怎样运行,当满足if判断条件时执行到break语句之后程序会跳出最里面那一层的for循环而不是跳过整个嵌套循环。补充:tempRandom.nextInt(100 - 50)函数的传参我做出了一些修改方便运行之后观察到成绩最好的数据记录。
3、接下来task1函数后面需要筛选出成绩最好和成绩最差的学生数据。在前面第二个嵌套for循环语句我们已经计算出每一个学生的总成绩并将总成绩存入了totalScores数组中,所以我们只需要遍历一下该数组选取其中的最大值和最小值即可。具体实现方法是分别定义两个变量tempBestScore和tempWorstIndex用来保存当前的最好成绩和最差成绩,并且还定义了两个变量tempBestIndex和tempWorstScore用来记录最好成绩和最差成绩在总数据表data数组中的行下标。注意:在筛选成绩最好的学生总成绩时较为容易理解,但是在筛选成绩最差的学生总成绩时存在一些问题。tempWorstScore = m * upperBound + 1这段代码将将用于筛选成绩最差的这个起始界限定的太低了以至于筛选不出来成绩最差的学生数据,建议将其改成总成绩更加合理。
4、最后是一些列的条件打印程序,我们最初将tempBestIndex和tempWorstScore两个变量的初始值赋为-1。当没有筛选出最好成绩和最差成绩时就打印查找失败的相关提示信息,如果筛选出了就打印出该学生的各科成绩数据。
2.运行结果
3.总结
总体来说整个任务目标不是特别的复杂,实现起来也应该没有太大的问题。在今天的代码中的这个new创建对象的这个操作让我找到了之前学Java的那么一点点感觉了,也让我想起来Java的那几大特性如封装、继承和多态。