前言
此篇为前九天学习内容的一个简单测试,在自己使用Java编程实现一个功能的过程中也遇见了很多问题,证明上手和看懂别人的代码不在一个难度级。
一、测试题目
学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:
进行学生成绩的随机生成, 区间为 [50, 100].
找出成绩最好、最差的同学。但有挂科的同学不参加评比.
二、程序编程
相应的测试题目可以分解为几个简单步骤:
Step 1: set a table with m students and n courses.
Step 2: Generate random data
Step 3: Compute the total score of each student.
Step 4: Find the best and worst classmates. However, students who have failed subjects will not participate in the evaluation.
具体代码如下:
package basic;
import java.util.Arrays;
import java.util.Random;
/**
* This is the tenth code, also the first task.
*
* @author Weize 1025976860@qq.com
*/
public class Task {
public static void main(String args[]) {
task1();
}
//of main
/**
*********************
* Method unit test.
*********************
*/
public static void task1() {
//Step 1: set a table with m students and n courses.
//Step 2: Generate random data
//Step 3: Compute the total score of each student.
//Step 4: Find the best and worst classmates. However, students who have failed subjects will not participate in the evaluation.
int m=10;
int n=3;
int [][] Tdata=new int[m][n];
int maxGrades=100;
int minGrades=50;
int threshold=60;
Random tempRandom = new Random();
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
Tdata[i][j]=minGrades+tempRandom.nextInt(maxGrades-minGrades);
}//of for j
}//of for i
System.out.println("The data is:\r\n" + Arrays.deepToString(Tdata));//输出生成的数组
// Step 3. Compute the total score of each student.
int[]addTdata=new int[m];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if (Tdata[i][j] < threshold) {
addTdata[i] = 0;
break;
} // Of if
addTdata[i]+=Tdata[i][j];
}
}
System.out.println("The total scores are:\r\n" + Arrays.toString(addTdata));
//Step 4: Find the best and worst classmates.
// 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 * maxGrades + 1;
for (int i = 0; i < n; i++) {
// Do not consider failed students.
if (addTdata[i] == 0) {
continue;
} // Of if
if (tempBestScore < addTdata[i]) {
tempBestScore = addTdata[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 > addTdata[i]) {
tempWorstScore = addTdata[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(Tdata[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(Tdata[tempWorstIndex]));
} // Of if
}// Of task1
}// Of class Task1
3、遇到的问题
1、首先是格式的问题,主要体现在符号的使用,如;总写成,这些是对编程语言不熟悉造成的,还是要多写。
2、逻辑不通顺,不顺畅;电脑程序是一个很智能但同时也是很死板的事情,要按照它对对应的逻辑思维进行编程,不然运行的结果往往背道而驰。
3、语言过于复杂,不简洁。
反正就是写的太少
4、运行结果
总结
未完待续