日撸 Java 三百行 (day03 Java基础)

今天是java基础语法的收尾,以一个综合任务画句号。

综合任务一

学生的成绩存放于一个矩阵,其中行表示学生,列表示科目。如:第 0 行表示第 0 个学生的数学、语文、英语成绩。要求:

  1. 进行学生成绩的随机生成, 区间为 [50, 100].
  2. 找出成绩最好、最差的同学。但有挂科的同学不参加评比.(60分为及格线)

过程分析:数据结构选择二维数组,以行号 i 表示不同的学生,列号 j 表示不同的科目

代码:

package com.day03;

import java.util.Arrays;
import java.util.Random;
public class day03
{

    /**
     *********************
     * 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.
        // Set these 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+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.
        // Typical initialization for index: invalid value.    默认查询失败的下标:-1
        int tempBestIndex = -1;
        int tempWorstIndex = -1;
        // Typical initialization for best and worst values.  初始化最高分:0    最低分: m * upperBound + 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 worst student is No." + tempWorstIndex + " with scores: "
                    + Arrays.toString(data[tempWorstIndex]));
        } // Of if
    }// Of task1

}// Of class day03

 运行截图:

 代码分析:

        Arrays类中toString()方法   

         

        Random类中nextInt()方法   

 生成一个0-N之间的随机数,利用语句

data[i][j] = lowerBound+ tempRandom.nextInt(upperBound - lowerBound+1);

可以构造出50-100之间的随机数,关键在于能否取到边界值。

下面使用while循环检验边界值:

package com.day03;


import java.util.Random;
/**
 *********************
 * The entrance of the program.
 *
 *********************
 */
public class day03_helper {

    //maxtest 最大随机次数,超过默认取不到
    //lowerBound 下界
    //upperBound上界
    //target 目标随机值
    static int  maxtest=1000;


    public static void test(int lowerBound,int upperBound,int target) {
        // Here we have to use an object to generate random numbers.
        Random tempRandom = new Random();
        //判断边界能否取到边界值
        while(lowerBound + tempRandom.nextInt(upperBound - lowerBound+1)!=target){
                maxtest--;
                if(maxtest==0) {
                    System.out.println("随机失败,无法取到" + target);
                    break;
                }
        }//of while
        if(maxtest!=0)
            System.out.println("成功取到" + target);
        maxtest=1000;
    }  // of  test

    public static void main(String args[]) {
        int lowerBound = 50;
        int upperBound = 100;
        test(lowerBound,upperBound,100);
        test(lowerBound,upperBound,50);
        test(lowerBound,upperBound,88);
        test(lowerBound,upperBound,101);
        test(lowerBound,upperBound,49);
    }// Of main

}//of  day03_helper

运行效果:

 当然此题的边界判断比较简单,可以不必使用程序,有些时候的人脑总会有疏忽,不妨用程序来帮忙。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值