为什么我的比较功能不适用于最高输入值和最低输入值?

时间:2017-02-09 21:02:34

标签: java

好的朋友,我想弄清楚为什么" -1" (我的退出价值)进行比较,成为我的最低成绩,为什么" 100"不是我的最高年级。我确定这些原因很简单,但我看不到它们。

示例输入和输出如下:

欢迎来到Grader-ator!

输入0到100之间的成绩,或输入负数以退出。 100

输入0到100之间的成绩,或输入负数以退出。 90

输入0到100之间的成绩,或输入负数以退出。 80

输入0到100之间的成绩,或输入负数以退出。 70

输入0到100之间的成绩,或输入负数以退出。 -1

总成绩:340

总成绩是:4

A的数量:2

B的数量:1

C的数量:1

D的数量:0

F的数量:0

最高等级:90

最低等级:-1

平均成绩:85.0

代码在这里开始

import java.util.Scanner;

public class Lab7 
{

    /**
     * author: Nate Fuller
     * date: 9 Feb 17
     * This program takes in any number of grades and performs some calculations
     */

    public static int grade, totalNumOfGrades, totalSumOfGrades, highestGrade, lowestGrade,
    numberOf_As, numberOf_Bs, numberOf_Cs, numberOf_Ds, numberOf_Fs,
    oldGrade, newGrade;
    public static double averageGrade;
    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args) 
    {
        System.out.println("Welcome to Grader-ator!");
        System.out.println("Enter a grade from 0 to 100, " +
                "or enter a negative number to quit.");

        totalSumOfGrades = 0;

        grade = kb.nextInt();
        while(grade >= 0)
        {
            System.out.println("Enter a grade from 0 to 100, " +
                    "or enter a negative number to quit.");

            determineGrade();
        }
        printGrades();
    }

    public static void determineGrade()
    {
        //A
        if(grade >= 90 && grade <= 100)
        {
            numberOf_As++;
            increment();
            compare();
        }
        //B
        else if(grade >= 80 && grade <= 89)
        {
            numberOf_Bs++;
            increment();
            compare();
        }
        //C
        else if(grade >= 70 && grade <= 79)
        {
            numberOf_Cs++;
            increment();
            compare();
        }
        //D
        else if(grade >= 60 && grade <= 69)
        {
            numberOf_Ds++;
            increment();
            compare();
        }
        //F
        else if(grade >= 0 && grade <= 59)
        {
            numberOf_Fs++;
            increment();
            compare();
        }
        else
        {
            System.out.println("Please enter a number between 0 and 100.");
            grade = kb.nextInt(); 
        }

    }

    public static void increment()
    {
        totalNumOfGrades++;
        totalSumOfGrades += grade;
        grade = kb.nextInt();
    }

    public static void compare()
    {
        newGrade = grade;
        if(newGrade > oldGrade)
        {
            highestGrade = newGrade;
        }
        else if(newGrade <= oldGrade)
        {
            lowestGrade = newGrade;
        }
        oldGrade = newGrade;
    }

    public static void printGrades()
    {
        //Calculations
        averageGrade = totalSumOfGrades/totalNumOfGrades;

        System.out.println("Total sum of grades: " + totalSumOfGrades);
        System.out.println("Total Number of Grades is: " + totalNumOfGrades);
        System.out.println("Number of A's: " + numberOf_As);
        System.out.println("Number of B's: " + numberOf_Bs);
        System.out.println("Number of C's: " + numberOf_Cs);
        System.out.println("Number of D's: " + numberOf_Ds);
        System.out.println("Number of F's: " + numberOf_Fs);
        System.out.println("Highest Grade: " + highestGrade);
        System.out.println("Lowest Grade: " + lowestGrade);
        System.out.println("Average Grade: " + averageGrade);
    }
}

2 个答案:

答案 0 :(得分:0)

只有当newGrade大于等于0时才应执行整个if else块。最重要的是添加了if条件。这应该可以解决问题。

public static void compare()
{
   if(newGrade >=0 ) {
    newGrade = grade;
    if(newGrade > oldGrade)
    {
        highestGrade = newGrade;
    }
    else if(newGrade <= oldGrade)
    {
        lowestGrade = newGrade;
    }
    oldGrade = newGrade;
}
}

答案 1 :(得分:0)

要使您的计划有效,请从grade = kb.nextInt();方法中移除increment()行,并在else方法中删除determineGrade()块。

在您的主要方法中,进行以下更改:

grade = 0;
while(grade >= 0)
{
    System.out.println("Enter a grade from 0 to 100, " + 
               "or enter a negative number to quit.");
     grade = kb.nextInt();
     determineGrade();
}
printGrades();  

您的代码之间没有相应行为的原因是因为在处理输入等级之前,您从用户处获取新输入并丢失了进一步处理所需的旧值。