需要为此平均测试结果程序设置最大值(100)和最小值(0)

时间:2015-12-24 13:36:58

标签: java

需要为此平均测试结果程序设置最大值(100)和最小值(0)。我知道我需要使用'<'和'>'在我工作的某个地方,但我不确定如何/在哪里

import java.util.Scanner;

public class ExamResults {
    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);          
        System.out.println("Enter the 5 exam results");
        double ExamResult1 = 0.0;
        ExamResult1 = Double.parseDouble(keyboard.nextLine());
        double ExamResult2 = 0.0;
        ExamResult2 = Double.parseDouble(keyboard.nextLine());
        double ExamResult3 = 0.0;
        ExamResult3 = Double.parseDouble(keyboard.nextLine());
        double ExamResult4 = 0.0;
        ExamResult4 = Double.parseDouble(keyboard.nextLine());
        double ExamResult5 = 0.0;
        ExamResult5 = Double.parseDouble(keyboard.nextLine());
        double averageScore;                                    
        averageScore = ((ExamResult1 + ExamResult2 + ExamResult3 + ExamResult4 + ExamResult5)/5);
        System.out.println("The average Score is" + averageScore);
    }
}

2 个答案:

答案 0 :(得分:0)

尝试类似:

double min = Math.min(Math.min(ExamResult1, ExamResult2), ExamResult3);//similarly for others
double max = Math.max(Math.max(ExamResult1, ExamResult2), ExamResult3);//similarly for others

答案 1 :(得分:0)

我会这样做:

    Scanner keyboard = new Scanner(System.in);          
    System.out.println("Enter the 5 exam results");
    double[] examResults = new double[5];
    double total = 0.0;
    for (int i = 0; i < examResults.length; i++)
    {
        double value = Double.parseDouble(keyboard.nextLine());

        while (value < 0 || value  > 100)
        {
             System.out.println("Invalid score, try again");
             value = Double.parseDouble(keyboard.nextLine());
        }
        examResults[i] = value;
        total += value;
    }
    double averageScore;                                    
    averageScore = total / examResults.length;
    System.out.println("The average Score is" + averageScore);