1. 任务说明及代码注意点
1.1 任务说明
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
1). 进行学生成绩的随机生成,区间为 [50, 100]。
2). 找出成绩最好、最差的同学。但有挂科的同学不参加评比。
因为这里是测试,所以通过随机生成学生成绩,来减少输入的麻烦。随机则会使用到random函数,这里用到了Random.nextInt()方法,Random.nextInt(n)是生成一个随机的int值,该值是介于0到n之间的随机int值,它包含0但不包含n。(这里暂时只考虑成绩都是整型的情况)
Random tempRandom = new Random();
int n1 = tempRandom.nextInt(n); // Random range [0,n)
int n2 = a+tempRandom.nextInt(b); // Random range [a,b)
int n3 = a+tempRandom.nextInt(b+1); // Random range [a,b]
1.2 代码注意点
1.2.1 一般的初始化
下标的初试化一般为无效值,最值中的最大值一般初始化为可能的最小值,最小值一般初始化为可能的最大值加1。
// 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;
1.2.2 if不能连接
这里判断需要用两个if,而不能使用if-else来连接,是因为if-else是一个非此即彼的关系,但一个分数他可能即是最大值也可能是最小值,也就是说这个分数可能满足两个if的条件,不是一个非此即彼的关系。
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
} // Of if
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
} // Of if
2.总代码测试
package basic;
import java.util.Arrays;
import java.util.Random;
/**
* This is the tenth code, also the first task;
*
* @author Yunhua Hu yunhuahu0528@163.com.
*/
public class Task1 {
/**
*********************
* 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 = 5;
int m = 3;
int lowerBound = 50;
int upperBound = 101; // Should be 100. I use thisvalue 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(upperBound - lowerBound);
} // 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 beat 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
输出:
疑问:"为了随机数,迫不得已提前使用了 new 语句生成对象",不太明白这句话想表达的意思。