大家好,欢迎来到IT知识分享网。
0.主题
今天完成综合任务1,需要实现两个要求
- 学生成绩的随机生成, 区间为 [50, 100]
- 找出成绩最好、最差的同学。但有挂科的同学不参加评比
学生的成绩存放于一个矩阵中,矩阵中第 i i i行 j j j列的值含义为第 i i i个学生第 j j j门课的成绩。
1.分析
- 成绩存放
学生成绩放在一个矩阵中,可以用一个二维数组来表示该矩阵。 - 成绩生成
- 生成 [ 50 , 100 ] [ 50, 100 ] [50,100]内的伪随机数
调用java.util.Random类,使用其中的public int nextInt(int n)方法。该方法生成一个 [ 0 , n ] [ 0, n ] [0,n]范围内的随机整数,通过以下公式可以生成 [ l o w e r B o u n d , u p p e r B o u n d ] [ lowerBound, upperBound ] [lowerBound,upperBound]范围内的随机整数。
l o w e r B o u n d + t e m p R a n d o m . n e x t I n t ( u p p e r B o u n d − l o w e r B o u n d + 1 ) lowerBound + tempRandom.nextInt( upperBound – lowerBound + 1 ) lowerBound+tempRandom.nextInt(upperBound−lowerBound+1)
其中tempRandom是java.util.Random类的一个实例。 - 生成所有学生成绩
用二重for循环遍历二维数组,对每个位置使用上一步的方法生成 [ 50 , 100 ] [ 50, 100 ] [50,100]范围内的随机数,即可生成所有学生的成绩
- 生成 [ 50 , 100 ] [ 50, 100 ] [50,100]内的伪随机数
- 求成绩最好、最差的学生,挂科者不参与
- 求每个学生的总分
用二重for循环,每次将二维数组中的第 i i i行累加起来,即为第 i i i个同学的总成绩。由于挂科的同学不参与评比,故每次遍历到低于 60 60 60的分数便将该同学的总分记做 0 0 0分,这是一个特殊标记,然后不再统计该同学其他各科成绩,而是使用continue直接开始统计下一位同学的成绩。 - 求成绩最好学生
维持两个变量tempBestScore与tempBestIndex,分别表示遍历totalScores数组时遇到的暂时最高分与暂时最高分的下标。遇到挂科学生(总分标记为 0 0 0)时跳过,否则和totalScores[ i ]比较以更新tempBestScore与tempBestIndex。当totalScores数组遍历结束时,tempBestIndex要么为-1,表示所有学生都有挂科,要么为最高分的下标。 - 求成绩最差学生
与成绩最好学生类似,不再赘述。
- 求每个学生的总分
- 结果输出
根据tempBestIndex或tempWorstIndex的值,输出对应的结果。
2.程序
- 程序代码如下
package basic; import java.util.Arrays; import java.util.Random; 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 data with n students and m courses. int n = 9; int m = 3; int lowerBound = 50; int upperBound = 100; int threshold = 60; //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 + 1 ); } // 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. int tempBestIndex = -1; int tempWorstIndex = -1; 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 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 best student is No." + tempWorstIndex + " with scores: " + Arrays.toString( data[ tempWorstIndex ] ) ); } // Of if } // Of task1 } // Of class Task1
- 程序执行结果如下
可以看到程序输出了所期望的结果。
3.其他
- 了解了java.util.Random类,掌握了一种生成特定区间范围内伪随机整数的方法
- 对于记录某些特定位置或特定值的变量,初始化时应设为一个无效值,以作为特殊标记,当找不到符合要求的特定位置或值时可作为反馈。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/137471.html