如果声明有更简单的方法吗?

时间:2015-02-25 17:47:44

标签: java if-statement

我正在研究一个输入等级的java程序给出了输入的总数和平均值但是我很难弄清楚如何进行当前输入的最高等级我使用的是大量的嵌套else if语句,但必须有一个简单的方法来做到这一点,而不是键入100 else if if语句是我的代码。我发表评论,其中if语句开始确定最高等级

//Jonathan Towell
//This program will take grade inputs and then display the amount of A's B's C's D's and F's
import javax.swing.JOptionPane;


public class Grades 
{
public static void main(String[] args)
{
    JOptionPane.showMessageDialog(null, "Welcome to Jonathan's Gradebook");
    int A = 0, B = 0, C = 0, D = 0, F = 0;
    int totalGrades = 0, grade = 0, averageGrade = 0, highestGrade = 0;
    while (grade >= 0)
    {
    grade = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade followed by the enter key\n when finished enter -1"));
    if (grade <= 100 && grade >= 90)
    {
        A = A + 1;
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
        if (grade == 100) // Determines highest grade
        {
            highestGrade = 100;
        }
        else if (grade == 99)
        {
            highestGrade = 99;
        }
        else if (grade == 98)
        {
            highestGrade = 98;
        }
        else if (grade == 97)
        {
            highestGrade = 97;
        }
        else if (grade == 96)
        {
            highestGrade = 96;
        }
        else if (grade == 95)
        {
            highestGrade = 95;
        }
        else if (grade == 94);
        {
            highestGrade = 94;
        }
    }

    else if (grade > 100)
    {
        JOptionPane.showMessageDialog(null, "No extra credit for this assignment\n please enter a value less than 100");
    }
    else if (grade <= 89 && grade >= 80)
    {
        B = B + 1;
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
    }
    else if (grade <= 79 && grade >= 70)
    {
        C = C + 1;
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
    }
    else if (grade <= 69 && grade >= 60)
    {
        D = D + 1;
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
    }
    else if (grade <= 59 && grade >= 0)
    {
        F = F + 1;
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
    }
    }
    averageGrade = (averageGrade / totalGrades);
    JOptionPane.showMessageDialog(null, "Total number of grades entered was " + totalGrades + "\n The average grade was: " + averageGrade + " percent." + "\nA's: " + A + "\nB's: " + B + "\nC's: " + C + "\nD's: " + D + "\nF's: " + F + "\nThe highest grade earned was: " + highestGrade + " percent.");
    System.exit(0);
}


}

2 个答案:

答案 0 :(得分:2)

为了使这更易于管理,您可以考虑使用具有多态性的工厂模式,并将所有等级括号(if,else-if)在他们自己的单独类中实现。

答案 1 :(得分:1)

乔纳森,

你重复了很多代码。在每个if子句中,您重复相同的两行代码。为什么不在最后的if子句之外执行这两行代码。这两行重复:     totalGrades = totalGrades + 1;     averageGrade = averageGrade + grade;

关于巨大的if声明      if(等级== 100)//确定最高等级      {

将其替换为:     grade =(grade&gt; highestGrade?highestGrade:grade); //确定最高等级

你也可以使用:     highestGrade = Math.max(highestGrade,grade);

我还重新订购了部分代码。你的if语句说&#34;如果在100到90之间&#34;然后转到&#34;如果超过100&#34;。这简直令人困惑。我订购了#34;如果超过100&#34;然后&#34;如果介于100和90之间,那就更合乎逻辑了。

最终代码应如下所示:

public class Grades 
{
    public static void main(String[] args)
    {
        JOptionPane.showMessageDialog(null, "Welcome to Jonathan's Gradebook");
        int A = 0, B = 0, C = 0, D = 0, F = 0;
        int totalGrades = 0, grade = 0, averageGrade = 0, highestGrade = 0;
        while (grade >= 0)
        {
        grade = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade followed by the enter key\n when finished enter -1"));
        if (grade > 100) {
            JOptionPane.showMessageDialog(null, "No extra credit for this assignment\n please enter a value less than 100");
        } else if (grade <= 100 && grade >= 90) {
            A = A + 1;
            grade = (grade > highestGrade ? highestGrade : grade);// Determines highest grade
        } else if (grade <= 89 && grade >= 80) {
            B = B + 1;
        } else if (grade <= 79 && grade >= 70) {
            C = C + 1;
        } else if (grade <= 69 && grade >= 60) {
            D = D + 1;
        }  else if (grade <= 59 && grade >= 0) {
            F = F + 1;
        }
        totalGrades = totalGrades + 1;
        averageGrade = averageGrade + grade;
        averageGrade = (averageGrade / totalGrades);
        JOptionPane.showMessageDialog(null, "Total number of grades entered was " + totalGrades + "\n The average grade was: " + averageGrade + " percent." + "\nA's: " + A + "\nB's: " + B + "\nC's: " + C + "\nD's: " + D + "\nF's: " + F + "\nThe highest grade earned was: " + highestGrade + " percent.");
        System.exit(0);
    }

}

让我知道这对你来说是否更好。