java学习day10

119 阅读2分钟

day10 综合任务 1

10.1 题目解读

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

  • 1.初始化学生成绩(会涉及到随机生成数据的区间范围:Random)
  • 2.通过for循环来计算学生总成绩并排除挂科同学(借助:break,continue关键字)
  • 3.for循环+if判断来找出成绩最好和最差的学生

10.2 代码

package basic;

import java.util.Arrays;
import java.util.Random;
public class Task1 {
    public static void main(String[] args) {
        task1();
    }

    public static void task1(){
        //step1:Generate the data with n students and m courses.
        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);
            }
        }

        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;
                }

                totalScores[i] += data[i][j];
            }
        }

        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;
            }

            if (tempBestScore < totalScores[i]) {
                tempBestScore = totalScores[i];
                tempBestIndex = i;
            }

            // 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. I found this bug while setting upperBound = 65.
            if (tempWorstScore > totalScores[i]) {
                tempWorstScore = totalScores[i];
                tempWorstIndex = 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]));
        }

        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]));
        }
    }
}

在这里插入图片描述

自我小结

三大结构

不管在c或者java,都会涉及到三大结构:顺序结构,选择结构,分支结构;通过对这三大结构的组合使用就可以解决一些很复杂的问题。 (1) 顺序结构:即按照代码的书写顺序依次执行,不存在跳转或者判断

  • 要避免代码冗余,可以通过封装相应的方法或类来避免;例如day4中我们可以专门封装一个方法来判断是否为闰年,而避免每次都去重复写来判断
  • 需要注意变量的使用范围等。例如day4中main方法中变量tempYear,正因为代码会顺序执行,所以可以对tempYear多次赋值从而覆盖前面的值。

(2)选择结构:会根据指定的判定条件去执行不同的代码块内容,如在day3,day4:if结构;if-else结构;if-else if结构;day5:switch结构

  • 在选择结构中,要保证我们的判定条件是正确的

(3)循环结构:会根据指定的条件重复执行一段代码块内容,直到条件不符合跳出循环,如在day6-9中 for循环,while循环,除此之外还有do...while循环

  • 循环条件要保证条件是能正确跳出循环条件的,否则会导致程序进入死循环