日撸 Java 三百行(10天,综合任务1)

日撸 Java 三百行(10天,综合任务1)本文介绍了一个 Java 程序 用于生成学生科目成绩矩阵 并找出最高分和最低分的学生

大家好,欢迎来到IT知识分享网。

日撸 Java 三百行(10天,综合任务1)

注意:这里是JAVA自学与了解的同步笔记与记录,如有问题欢迎指正说明


关于消失的第09天

根据老师的任务清单的顺序来看,今天应该是第九天的while循环任务,但是while语句本身与C系列语言使用没有区别,循环语句的思想在第六天的for语句已经讲述了,前面几天通过矩阵也见识了循环语句的部分运用,因此今天跳过第九天直接进入第十天

一、要求与分析

原文对于问题的描述如下:

1.数据结构分析

每个学生的科目都是三科,不会多不会少,因此多个学生之间的记录是定长记录,因此可以考虑使用固定的一个m*3二维数组来记录所有学生的成绩。此外,题目要求记录成绩最好和最差的同学,因此,可以考虑使用一个数组记录每个同学的成绩和,然后通过成绩统计最高成绩与最低成绩。

注:就实现统计最好最差同学来说,这里的求和数组不是必要的,可以在设计学生成绩时就完成相关统计。因此代码是可以简化的,只是此处为了练习java代码内容故尽可能让数据结构完善。

2.方法准备

二、代码实现分析

1.初始化与成绩矩阵

代码如下

// Step 1. Generate the date with n student and m course. // Set these value by yourself. int n = 10; int m = 3; int lowerBound = 50; int upperBound = 101; int threshold = 60; // Here we have to use an object to generate random numbers Random temRandom = 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 + temRandom.nextInt(upperBound - lowerBound); } // Of for j } // Of for i 

2.创建总成绩数组

代码如下

// 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 

3.查询成绩极值

代码如下

// Step 3. Find the best and 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 (totalScores[i] == 0) { 
     continue; } // Of if if (tempBestScore < totalScores[i]) { 
     tempBestScore = totalScores[i]; tempBestIndex = i; } // Of if // Attention: This if statememt 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 

三、代码总览与数据运行分析

代码如下

package basic; import java.util.Arrays; import java.util.Random; / * This is the tenth code, also the first task. * * @author Xingyi Zhang @.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 date with n student and m course. // Set these value by yourself. int n = 10; int m = 3; int lowerBound = 50; int upperBound = 101; // Should be 100. I use this value for testing. int threshold = 60; // Here we have to use an object to generate random numbers Random temRandom = 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 + temRandom.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 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 (totalScores[i] == 0) { 
      continue; } // Of if if (tempBestScore < totalScores[i]) { 
      tempBestScore = totalScores[i]; tempBestIndex = i; } // Of if // Attention: This if statememt 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 sudent number and score. if (tempBestIndex == -1) { 
      System.out.println("Cannot find best student. All student 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 student 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 

一次运行结果如下
在这里插入图片描述
在一次运行后数据如我们预期那样,对于不及格的学生,其总分是按照0处理的。
为了验证我们预先的特殊情况,我们先将随机生成的分数上限下调到66,让我们的分数随机数更容易落到不及格:
在这里插入图片描述
可以发现,因为每个人存在不及格学科,因此大家总分都不及格,因此,这里大家的分数在比较时都跳过了,出现了“无最高分,无最低分”的人为规定条件。




总结

· 关于求最大最小值的联想

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/105737.html

(0)
上一篇 2026-02-15 14:25
下一篇 2026-02-16 07:48

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信