这是对之前java学习代码的一个总结,应用了之前所学的知识。需要好好理解融会贯通。
package java1to10;
import java.util.Arrays;
import java.util.Random;
public class D10_Task1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
task1();
}
public static void task1() {
int n = 10;
int m = 3;
int lowBound = 30;
int upBound = 80;
int threshold = 40;
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] = lowBound + tempRandom.nextInt(upBound - lowBound);
}
}
System.out.println("成绩是:\r\n" + Arrays.deepToString(data));
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;
}
totalScores[i] += data[i][j];
}
}
System.out.println("总成绩是:\r\n" + Arrays.toString(totalScores));
int tempBestIndex = 1;
int tempWorstIndex = 1;
int tempBestScore = 2;
int tempWorstScore = m * upBound + 1;
for (int i = 0; i < n; i++) {
if (totalScores[i] == 0) {
continue;
}
if (tempBestScore < totalScores[i]) {
tempBestScore = totalScores[i];
tempBestIndex = i;
}
if (tempWorstScore > totalScores[i]) {
tempWorstScore = totalScores[i];
tempWorstIndex = i;
}
}
if (tempBestIndex == -1) {
System.out.println("没有发现成绩优秀的学生。");
} else {
System.out.println(String.format("第%s个学生,成绩优异,成绩是:%s", tempBestIndex, Arrays.toString(data[tempBestIndex])));
}
if (tempWorstIndex == -1) {
System.out.println("也没有发现特别差的学生。");
} else {
System.out.println(String.format("第%s个学生,成绩较差,成绩是:%s", tempWorstIndex, Arrays.toString(data[tempWorstIndex])));
}
}
}
输出结果:
成绩是:
[[33, 37, 37], [70, 48, 51], [77, 53, 62], [68, 56, 40], [36, 54, 65], [52, 63, 63], [63, 37, 34], [54, 46, 67], [60, 50, 44], [77, 30, 48]]
总成绩是:
[0, 169, 192, 164, 0, 178, 0, 167, 154, 0]
第2个学生,成绩优异,成绩是:[77, 53, 62]
第8个学生,成绩较差,成绩是:[60, 50, 44]