简体   繁体   English

用户输入的总和,平均,最低和最高得分(条件控制结构)

[英]Sum, Average, Lowest and Highest scores of user input (Conditional Control Structures)

I have the following homework assignment. 我有以下作业。 Our teacher hasn't taught us well, so I need extra help. 我们的老师教得不好,所以我需要额外的帮助。

Write a Java program that will input five scores of a student in five quizzes. 编写一个Java程序,它将在五个测验中输入五个学生的分数。 Output the sum and average of five scores. 输出五个分数的总和和平均值。 Determine and display the highest and lowest score. 确定并显示最高和最低分数。

This is how I am stuck now. 这就是我现在被困住的方式。 I only have up to Q3 last time and it only shows the average and the lowest score with no highest score. 上次我只有Q3,它只显示平均分数和最低分数,而没有最高分数。 Kindly help me make this work! 请帮助我完成这项工作!

import java.util.Scanner;
import java.io.*;

class Quizzes
{

    public static void main(String[] args) {
    int Q1, Q2, Q3, Q4, Q5;
    double average;

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter Quizzes: ");
    Q1 = keyboard.nextInt();
    Q2 = keyboard.nextInt();
    Q3 = keyboard.nextInt();
    Q4 = keyboard.nextInt();
    Q5 = keyboard.nextInt();

    average = (Q1 + Q2 + Q3 + Q4 + Q5) / 5;
    System.out.println("Average Score is: " + average);

    if (Q1 < Q2 && Q1 < Q3) {
        System.out.println("The lowest score is: " + Q1);
    } else if (Q2 < Q1 && Q2 < Q3) {
        System.out.println("The lowest score is: " + Q2);
    } else if (Q3 < Q1 && Q2 < Q3) {
        System.out.println("The lowest score is: " + Q3);
    } else if (Q1 > Q2 && Q1 > Q3) {
        System.out.println("The highest score is: " + Q1);
    } else if (Q2 > Q1 && Q2 > Q3) {
        System.out.println("The highest score is: " + Q2);        
    } else if (Q3 > Q3 && Q1 > Q2) {
        System.out.println("The highest score is: " + Q3);
    } else {
        System.out.println("The lowest score is: " + Q3);
    }

  }
}

In your current solution you have a couple of basic issues such as else if (Q3 > Q3) . 在当前解决方案中,您有几个基本问​​题,例如else if (Q3 > Q3)

You also don't check for equality ie if (Q2 >= Q3) . 您也不会检查是否相等,即if (Q2 >= Q3)

The major issue with your current technique is that you have a load of if/else statements, a better solution would be to use an Array like below: 当前技术的主要问题是您有大量的if / else语句,更好的解决方案是使用如下所示的Array:

public class Quizzes {

    public static void main(String[] args) {

        double Q1, Q2, Q3, Q4, Q5;
        double average = 0;
        double max = 0;
        double count = 0;
        double[] userInput = new double[5];

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter Quizzes: ");
        System.out.println("");
        Q1 = keyboard.nextInt();
        userInput[0] = Q1;
        Q2 = keyboard.nextInt();
        userInput[1] = Q2;
        Q3 = keyboard.nextInt();
        userInput[2] = Q3;
        Q4 = keyboard.nextInt();
        userInput[3] = Q4;
        Q5 = keyboard.nextInt();
        userInput[4] = Q5;
        keyboard.close();

        for (int i = 0; i < userInput.length; i++) {
            count = count + userInput[i];
            average = count / 5;
        }
        System.out.println("Average Score is: " + average);
        Arrays.sort(userInput);
        System.out.println("The lowest score is: " + userInput[0]);
        max = userInput[userInput.length - 1];
        System.out.println("The highest score is: " + max);
    }

}

EDIT: 编辑:

Now that you have informed us you cannot use an Array then below code will do the job without modifying your original method too much: 既然您已经通知我们您不能使用数组,那么下面的代码就可以完成这项工作,而无需过多修改您的原始方法:

public class Quizzes {

    public static void main(String[] args) {

        double Q1, Q2, Q3, Q4, Q5;
        double average = 0;
        double lowest = 0;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter Quizzes: ");
        System.out.println("");
        Q1 = keyboard.nextDouble();
        Q2 = keyboard.nextDouble();
        Q3 = keyboard.nextDouble();
        Q4 = keyboard.nextDouble();
        Q5 = keyboard.nextDouble();
        keyboard.close();

        average = (Q1 + Q2 + Q3 + Q4 + Q5) / 5;
        System.out.println("Average Score is: " + average);

        if (Q1 <= Q2 && Q1 <= Q3 && Q1 <= Q4 && Q1 <= Q5) {
            lowest = Q1;
        } else if (Q2 <= Q1 && Q2 <= Q3 && Q2 <= Q4 && Q2 <= Q5) {
            lowest = Q2;
        } else if (Q3 <= Q1 && Q3 <= Q2 && Q3 <= Q4 && Q3 <= Q5) {
            lowest = Q3;
        } else if (Q4 <= Q1 && Q4 <= Q2 && Q4 <= Q3 && Q4 <= Q5) {
            lowest = Q4;
        } else if (Q5 <= Q1 && Q5 <= Q2 && Q5 <= Q3 && Q5 <= Q4) {
            lowest = Q5;
        }

        System.out.println("The lowest score is: " + lowest);
        // THEN DO SIMILAR TO GET HIGHEST SCORE
    }
}

You can also insert these values into an array, go over this array and look for the min and max values. 您也可以将这些值插入到数组中,遍历该数组并查找最小值和最大值。

Like this: 像这样:

int[] arr = new int[5];
arr[0] = Q1;
arr[1] = Q2;
arr[2] = Q3;
arr[3] = Q4;
arr[4] = Q5;


int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

for (int i=0; i<arr.length; i++){

    min = Math.min(min, arr[i]);
    max = Math.min(max, arr[i]);

}

If you're not allowed to use arrays and/or loops, here's one way to do it. 如果不允许使用数组和/或循环,则这是一种方法。 You can see why arrays make it more concise; 您会明白为什么数组使它更简洁; you would not want to do this for 100 questions. 您不想针对100个问题进行此操作。 I have left out computation of the maximum, since it's trivially similar. 我忽略了最大值的计算,因为它微不足道。

int min = Integer.MAX_VALUE;
if (Q1 < min) {
    min = Q1;
}
if (Q2 < min) {
    min = Q2;
}
if (Q3 < min) {
    min = Q3;
}
if (Q4 < min) {
    min = Q4;
}
if (Q5 < min) {
    min = Q5;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM