学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
- 进行学生成绩的随机生成, 区间为 [50, 100]。
- 找出成绩最好、最差的同学。但有挂科的同学不参加评比。
代码如下:
package a10;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author hengyuzuo
*
*/
public class MissionTODO {
/**
* ***********************
* Classification
* @param args Not used now.
* ***********************
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
classification();
}// Of main
/**
* ************************
* Method unit test
* ************************
*/
public static void classification() {
// TODO Auto-generated method stub
int[][] tempMatrix = new int [10][3];
Random tempRandom = new Random();
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = 50 + tempRandom.nextInt(50);
}// Of j
}// Of i
System.out.println("Student's grades are : \r\n" + Arrays.deepToString(tempMatrix));
int[] totalScore = new int [10];
int threshold = 60;
int max = totalScore[0];
int min = totalScore[0];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
if (tempMatrix[i][j] < threshold) {
totalScore[i] = 0;
break;
}// Of if
totalScore[i] += tempMatrix[i][j];
}// Of for j
if (totalScore[i] > max) {
max = totalScore[i];
}// Of if
if (totalScore[i] < min) {
min = totalScore[i];
}// Of if
}// Of for i
System.out.println("The total score of the student is : \r\n" + Arrays.toString(totalScore));
System.out.println("The max is : \r\n" + max);
System.out.println("The min is : \r\n" + min);
}// Of classification
}// Of MissionTODO
运行结果:
Student's grades are :
[[77, 78, 63], [71, 83, 63], [71, 87, 83], [82, 94, 52], [77, 72, 84], [73, 95, 76], [72, 65, 70], [94, 65, 89], [50, 88, 80], [94, 53, 55]]
The total score of the student is :
[218, 217, 241, 0, 233, 244, 207, 248, 0, 0]
The max is :
248
The min is :
0
By the way:
Eclipse里//TODO 的用法是方便查找,快捷键ctrl + o
java数组的输出方式:
- Arrays.toString 一维数组
- Arrays.deepToString 多维数组
后来发现,在查找最小值时,并没有排除0的存在且没有学生序号,遂按照@minfanPHD的重写了
代码:
package a10;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author hengyuzuo
*
*/
public class MissionTODO2 {
/**
* ********************
* The entrance of the program.
* @param args
* ********************
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
classification();
}// Of main
/**
* **********************
* Method unit test.
* **********************
*/
public static void classification() {
//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 = 100;
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 id : \r\n" + Arrays.deepToString(data));
//Step 2.Compute the total score of each student.
int[] totalScore = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (data[i][j] < threshold) {
totalScore[i] = 0;
break;
}// Of if
totalScore[i] += data[i][j];
}// Of for j
}// Of for i
System.out.println("The total score is: \r\n" + Arrays.toString(totalScore));
//Step 3.Find the best and the 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 (totalScore[i] == 0) {
continue;
}// Of if
if (tempBestScore < totalScore[i]) {
tempBestScore = totalScore[i];
tempBestIndex = i;
}// Of if
//Attention: This is statement cannot be combined with the last one
//using "else if", because a student can be both the best and the worst
//found this bug while setting upperBound = 65.
if (tempWorstScore > totalScore[i]) {
tempWorstScore = totalScore[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 best student.All students have failed.");
}else {
System.out.println("The Worst student is No." + tempWorstIndex + " with scores: "+ Arrays.toString(data[tempWorstIndex]));
}// Of if
}// Of classification
}// Of MissionTODO2
运行结果:
The data id :
[[56, 75, 68], [67, 81, 51], [93, 60, 60], [76, 97, 92], [70, 73, 91], [98, 63, 60], [51, 64, 83], [81, 79, 64], [82, 53, 67], [89, 85, 51]]
The total score is:
[0, 0, 213, 265, 234, 221, 0, 224, 0, 0]
The best student is No.4 with scores: [76, 97, 92]
The Worst student is No.2 with scores: [93, 60, 60]
小结:
- 经常在 Eclipse 中使用鼠标右键 source -> format, 自动调整程序的格式
- 使用了 continue, 它是指继续跳过本次循环后面的代码,直接进入下一次循环. 而 break 是跳出整个循环体.
- switch 语句里, 如果没遇到 break, 就会继续往后执行
- 多个单词构成一个名字, 如果是变量名、方法名,则第一个单词首字母大写;如果是类名,则所有单词首字母大写
- nextInt 是 java.util.Random 的一种函数(功能、方法),它用于产生下一个随机整数。如果你要下一个随机实数,就应该写 nextDouble().
- break 是针对循环体,或者 switch 语句的. 这里使用 return 直接返回, 可以无视该方法内的其余任何代码. 即使在多重循环内部, return 都是直接返回,而不会一级一级地退出.
- 在public static fianl int ()中final 表示不可更改, 也即常数. 使用常数的好处主要在于: 如果你想修改它的值,只需要在定义这里修改就行,而不用在程序的不同地方修改