Java重开第十天-Task1

一.需求分析

二.代码展示

三.总结

一.需求分析

学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。
如:第 0 行表示第 0 个学生的数学、语文、英语成绩。
要求:
进行学生成绩的随机生成, 区间为 [50, 100]. 找出成绩最好、最差的同学。但有挂科的同学不参加评比。

1.随机数需要使用Java中util的Random类生成对应的对象,来实现随机数生成在这里插入图片描述
注意: 对于种子相同的Random对象,生成的随机数序列是一样的。

在这里插入图片描述

2.矩阵实现对每个学生的每门成绩的赋值。

 for (int i = 0; i < n; i++) {                                                       
     for (int j = 0; j < m; j++) {                                                   
         data[i][j] = lowerBound + tempRandom.nextInt(upperBound - lowerBound);      
     }//of for j                                                                     
 }//of for i                                                                         

3.但有挂科的同学不参加评比

 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                                  

二.代码展示

package basic;                                                                                                
                                                                                                              
import java.util.Arrays;                                                                                      
import java.util.Random;                                                                                      
                                                                                                              
/**                                                                                                           
 * @author Donghao Xu                                                                                         
 */                                                                                                           
public class Task1 {                                                                                          
    /**                                                                                                       
     * the entrance of  the program.                                                                          
     *                                                                                                        
     * @param args not used now.                                                                              
     */                                                                                                       
    public static void main(String[] args) {                                                                  
        //step 1 generate the data with n students and m courses                                              
        //set theses values by yourself                                                                       
        int n = 10;                                                                                           
        int m = 3;                                                                                            
        int lowerBound = 50;                                                                                  
        int upperBound = 100;                                                                                 
        int threshold = 60;                                                                                   
        //here we have to 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);                        
            }//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 the 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 statement can not 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("Can not find best student.All students have failed");                         
        } else {                                                                                              
            System.out.println("The best student index is No." + tempBestIndex + "with scores:" +             
                    Arrays.toString(data[tempBestIndex]));                                                    
        }//of if                                                                                              
        if (tempWorstIndex == -1) {                                                                           
            System.out.println("Can not find worst student.All students have failed");                        
        } else {                                                                                              
            System.out.println("The worst  student index is No." + tempWorstIndex + "with scores:" +          
                    Arrays.toString(data[tempWorstIndex]));                                                   
        }//of if                                                                                              
    }//of task1                                                                                               
}//of class Task1                                                                                             

三.总结

完成了一个小任务之后,觉得代码中的注释很重要,花括号的位置应该使得代码易于阅读,
在for循环里的break和continue的使用,可见第九天的区别分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值