/** * This is the ninth code. Names and comments should follow my style strictly. * * @author Shihuai Wen wshysxcc@outlook.com. */ publicclassWhileStatement {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { whileStatementTest(); }// Of main
/** ********************* * The sum not exceeding a given value. ********************* */ publicstaticvoidwhileStatementTest() { inttempMax=100; inttempValue=0; inttempSum=0;
/** * This is the tenth code, also the first task. * * @author Shihuai Wen wshysxcc@outlook.com. */ publicclassTask1 {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { task1(); }// Of main
/** ********************* * Method unit test. ********************* */ publicstaticvoidtask1() { // Step 1. Generate the data with n students and m courses. // Set these values by yourself. intn=10; intm=3; intlowerBound=50; intupperBound=100; intthreshold=60;
// Here we have to use an object to generate random numbers. RandomtempRandom=newRandom(); int[][] data = newint[n][m]; for (inti=0; i < n; i++) { for (intj=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 = newint[n]; for (inti=0; i < n; i++) { for (intj=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. inttempBestIndex= -1; inttempWorstIndex= -1; // Typical initialization for best and worst values. // They must be replaced by valid values. inttempBestScore=0; inttempWorstScore= m * upperBound + 1; for (inti=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. 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