Java:找到最高和最低分数

时间:2017-11-27 16:16:55

标签: java

我正在努力解决我在Java类中遇到的问题。首先是我对该计划的指示,以便您了解我正在尝试做什么:

编写一个允许用户输入任意数量的测试分数的应用程序,直到用户输入999.如果输入的分数小于0或大于100,则显示相应的消息并且不使用分数。输入分数后,显示输入的分数,高分,最低分和算术平均值。

---有几件事情出了问题。如果if语句不等于1或999,我的if语句没有正确验证答案。它是第一次运行,但是如果我把答案放错了,它会转到while语句而不是提示用户重新输入1号或999号。 ---其次,我不能得到最低分,但最高分。

这是我的代码:

package chp6homework;
import java.util.Scanner;
public class TestScoreStatistics {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int testScore;
        int totalTests = 0;
        int totalSum = 0;
        int answer;
        double average = 0;
        int hightScore = 0;
        int lowestScore = 0;

        // Prompts user for input
        System.out.print("Enter 1 to input a score.\n");
        System.out.print("Enter 999 to get results and exit. >> ");
        answer = input.nextInt();

        if(answer != 1 && answer != 999)
        {
            System.out.print("Invaild number!\n");
            System.out.print("Enter 1 to input a score.\n");
            System.out.print("Enter 999 to get results and exit. >> ");
            answer = input.nextInt();
        }

         while(answer == 1)
         {
            System.out.print("Please enter a test score >> ");
            testScore =  input.nextInt();

            if(testScore < 0 || testScore > 100)
            {
                System.out.print("Invalid score! \n");
                System.out.print("please enter a score that is not less than zero and" +
                        " and not more than 100. >> ");
                testScore =  input.nextInt();
            }
            totalSum = totalSum + testScore;
            ++totalTests;

            lowestScore = testScore;

            if(testScore > hightScore)
            {
                hightScore = testScore;
            }
            else if(testScore < lowestScore) 
            {
                lowestScore = testScore;
            }

            System.out.println("Your score was processed.");
            System.out.print("Enter 1 to input a score.\n");
            System.out.print("Enter 999 to get results and exit.\n");
            answer = input.nextInt();

         }

        average = totalTests * totalSum;

        System.out.println("The number of tests: " + totalTests);
        System.out.println("The total tests are: "+ totalSum);
        System.out.println("The average of scores is: " + average);
        System.out.println("Highest score: " + hightScore);
        System.out.println("Lowest score: " + lowestScore);
        }
}

1 个答案:

答案 0 :(得分:0)

要获得心理概述,请使用抽象:

<% parts.each do |part| %>

由于这是家庭作业,我把剩下的留给你。