如何修复此Java代码,以使其不仅适用于A级,而且还适用于所有年级?

时间:2019-03-03 15:36:06

标签: java

我应该改变什么?

import javax.swing.JOptionPane;

    public class Grades
    {
        public String scoreToGrade( int score )
        {
            String grade = "";
            if( score <= 100 )
            {
                if( score >= 90 )
                    grade = "A";

            }
            else if( score <= 89 )
            {
                if( score >= 80 )
                    grade = "B";
                    return grade;

            }
            else if( score <= 79 )
            {
                if( score >= 70 )
                    grade = "C";
             return grade;
            }
            else if( score <= 69 )
            {
                if( score >= 60 )
                    grade = "D";
             return grade;
            }
            else if( score <= 59 )
            {
                if( score >= 50 )
                    grade = "E";
               return grade;
            }
             else if( score <= 49 )
            {
                if( score >= 0 )
                    grade = "F";
          return grade;
            }
            else
            {
                grade = "Error: score " + score;
            }
            return grade;
        }

        public static void main( String[] args )
        {
            String input = JOptionPane.showInputDialog( "Score: " );
            int score = Integer.parseInt( input );
            Grades t = new Grades();
            String grade = t.scoreToGrade( score );
            JOptionPane.showMessageDialog( null,
                    "For " + score + " points, your grade is:  " + grade );
        }
    }

我认为问题在于退学等级;我试图增加一个成绩等级。在每个年级以下的语句,但也不起作用。 我认为问题在于退学等级;我试图增加一个成绩等级。在每个年级以下的语句,但这也不起作用。

3 个答案:

答案 0 :(得分:0)

这是一个快速的解决方案:

import javax.swing.JOptionPane;

public class Grades
{
    public String scoreToGrade( int score )
    {
        String grade;
        if( score < 50 ) return "F";
        else if( score >= 50 && score < 60) return "E";
        else if( score >= 60 && score < 70) return "D";
        else if( score >= 70 && score < 80) return "C";
        else if( score >= 80 && score < 90) return "B";
        else if( score > 90) return "A";
        else
        {
            grade = "Error: score " + score;
        }
        return grade;
    }

    public static void main( String[] args )
    {
        String input = JOptionPane.showInputDialog( "Score: " );
        int score = Integer.parseInt( input );
        Grades t = new Grades();
        String grade = t.scoreToGrade( score );
        JOptionPane.showMessageDialog( null,
                "For " + score + " points, your grade is:  " + grade );
    }
}

答案 1 :(得分:-1)

我认为您只需要在所有if语句两边加上大括号即可。例如

import javax.swing.JOptionPane;

public class Grades
{
public String scoreToGrade( int score )
{
    String grade = "";
    if( score <= 100 )
    {
        if( score >= 90 ){
            grade = "A";
            return grade;
        }
    }
    else if( score <= 89 )
    {
        if( score >= 80 ) {
            grade = "B";
            return grade;
        }

    }
    else if( score <= 79 )
    {
        if( score >= 70 ) {
            grade = "C";
            return grade;
        }
    }
    else if( score <= 69 )
    {
        if( score >= 60 ) {
            grade = "D";
            return grade;
        }
    }
    else if( score <= 59 )
    {
        if( score >= 50 ) {
            grade = "E";
            return grade;
        }
    }
    else if( score <= 49 )
    {
        if( score >= 0 ) {
            grade = "F";
            return grade;
        }
    }
    else
    {
        grade = "Error: score " + score;
    }
    return grade;
}

public static void main( String[] args )
{
    String input = JOptionPane.showInputDialog( "Score: " );
    int score = Integer.parseInt( input );
    Grades t = new Grades();
    String grade = t.scoreToGrade( score );
    JOptionPane.showMessageDialog( null,
        "For " + score + " points, your grade is:  " + grade );
}
}

虽然可能会缺少其他东西。

答案 2 :(得分:-1)

Turing85给了您很好的建议。我建议您遵循此步骤,以了解代码中出了什么问题。

但是要在这种特殊情况下为您提供帮助,您的代码无法在其他任何年级工作的问题就是您构建API条件的方式。

请考虑以下代码段:

if

如果 if( score <= 100 ) { if( score >= 90 ) grade = "A"; } else if( score <= 89 ) { if( score >= 80 ) grade = "B"; return grade; } score,则将>=90设置为grade。但是,如果A不是score而是>=90,那么它将不会进入将其设置为<=100的{​​{1}}块,因为它将直接从else if条件中走出B阶梯。

因此,您应该按以下方式重构方法:

else if