简体   繁体   English

为什么它返回错误的字符串?

[英]Why does it return the wrong String?

I'm new to Java, so apologies beforehand.我是 Java 的新手,所以事先道歉。

I'm making a method that takes the exam mark of a student (int mark) and returns a grade (String grade).我正在制作一种获取学生考试成绩(int mark)并返回成绩(字符串成绩)的方法。 But it sometimes returns the wrong grade.但它有时会返回错误的成绩。

public String calculateGrade(int mark) {
        String grade = "";
        if (mark < 40) {
            return ("Fail"); 
            }
        else if (mark < 50) {
            return ("3rd");
            }
        else if (mark < 60) {
            return ("2ii");
            }
        else if (mark < 70) {
            return ("2i");
            }
        else if (mark >= 70) {
            return ("1st"); 
            }
        else if (mark < 0) {
            return ("Invalid mark");
            }
        else if (mark > 100) {
            return ("Invalid mark"); 
            }
        return grade;
        }
System.out.println(labExample.calculateGrade(50));

The problem is that whenever i put in a value -1 it returns as "Fail" instead of "Invalid code."问题是,每当我输入一个值 -1 时,它都会返回“失败”而不是“无效代码”。 The same goes for over 100 as it returns as "1st" instead of "Invalid code."超过 100 个也是如此,因为它返回“1st”而不是“无效代码”。

Even though I've stated what to return if the grade is <0 or >100 it seems to ignore it.即使我已经说明了如果等级<0或>100要返回什么,它似乎忽略了它。

Any help or pointers would be great.任何帮助或指示都会很棒。 Thanks.谢谢。

The problem is that whenever i put in a value -1 it returns as "Fail" instead of "Invalid code.""*问题是,每当我输入一个值 -1 时,它都会返回“失败”而不是“无效代码”。”*

-1 is < 40 , so your first if 's condition is true. -1 < 40 ,所以你的第一个if条件为真。 That means your return ("Invalid mark");这意味着您的return ("Invalid mark"); code runs, which terminates the method — none of the rest of the code is executed.代码运行,这将终止该方法——代码的 rest 均未执行。 return doesn't just set a return value but continue with subsequent statements, it ends the method call. return不仅设置返回值,而且继续执行后续语句,它结束了方法调用。

Also note that you have a logical error later, you have a series of else if statements leading up to else if (mark < 70) followed by if (mark >= 70) .另请注意,稍后您有一个逻辑错误,您有一系列else if语句导致else if (mark < 70)后跟if (mark >= 70) That >= 70 will always be true. >= 70永远是正确的。 If mark were < 70 , the previous if condition would have been true and you would never have got there.如果mark < 70 ,则之前的if条件为真,您将永远不会到达那里。

The correct way to do this is to check in order , probably (in this case) from lowest to highest, but perhaps with the "Invalid mark" check up front.执行此操作的正确方法是按顺序检查,可能(在这种情况下)从最低到最高,但可能会预先检查“无效标记”。

Other notes:其他注意事项:

  • You don't need () around return values, return isn't a function/method.您不需要()围绕返回值, return不是函数/方法。
  • You don't need else if you're doing return in the if bodies.如果您在if正文中return ,则不需要else
  • You never used grade so there's no reason to have it in the code您从未使用过grade ,因此没有理由在代码中使用它

So:所以:

public String calculateGrade(int mark) {
    if (mark < 0 || mark > 100) {
        return "Invalid mark";
    }
    if (mark < 40) {
        return "Fail"; 
    }
    if (mark < 50) {
        return "3rd";
    }
    if (mark < 60) {
        return "2ii";
    }
    if (mark < 70) {
        return "2i";
    }
    return "1st"; 
}

Or assigning to grade and returning it at the end instead, which some consider best practice (and others consider unnecessary complication):或者分配给grade并在最后返回,有些人认为这是最佳实践(而另一些人认为不必要的复杂性):

public String calculateGrade(int mark) {
    String grade; // ** Note I don't do = ""; I want the compiler to warn me if I try to use it without assigning to it

    if (mark < 0 || mark > 100) {
        grade = "Invalid mark";
    }
    else if (mark < 40) {
        grade = "Fail"; 
    }
    else if (mark < 50) {
        grade = "3rd";
    }
    else if (mark < 60) {
        grade = "2ii";
    }
    else if (mark < 70) {
        grade = "2i";
    }
    else {
        grade = "1st"; 
    }
    return grade;
}

Note I needed the else s there, because I'm not return ing in the if bodies.请注意,我需要else那里,因为我没有在if正文中return

Your logic order is wrong.你的逻辑顺序是错误的。 Correct logic is正确的逻辑是

public String calculateGrade(int mark) {
    String grade = "";
     if (mark < 0) {
        return ("Invalid mark");
        }
    else if (mark > 100) {
        return ("Invalid mark"); 
        }
    else if (mark < 40) {
        return ("Fail"); 
        }
    else if (mark < 50) {
        return ("3rd");
        }
    else if (mark < 60) {
        return ("2ii");
        }
    else if (mark < 70) {
        return ("2i");
        }
    else if (mark >= 70) {
        return ("1st"); 
        }
    return grade;
    }

-1 < 100 is true: so it returns "Fail" -1 < 100 为真:所以它返回“失败”

101 >= 70 is true: so it returns "1st" 101 >= 70 为真:所以它返回“1st”

if doing it with IfEse: Check your borders first.如果使用 IfEse:首先检查您的边界。 Then do the grades.然后做成绩。

You need to sort also the order of the various IF.您还需要对各种 IF 的顺序进行排序。 In other words it matches the first valid IF.换句话说,它匹配第一个有效的 IF。

Another possible option, to avoid sorting the IFs, could be to check if like为了避免对 IF 进行排序,另一种可能的选择是检查是否喜欢

public String calculateGrade(int mark) {
        if (mark >= 0 && mark < 40) {
            return "Fail"; 
         }
        else if (mark >= 40 && mark < 50) {
            return ("3rd");
            }
        else if (mark >= 50 && mark < 60) {
            return ("2ii");
            }
        else if (mark >= 60 && mark < 70) {
            return ("2i");
            }
        else if (mark >= 70 && mark < 100) {
            return ("1st"); 
            }            
         return ("Invalid mark");
        }

Your problem is probably the way you think of this problem.你的问题可能就是你对这个问题的看法。

You should first think about all the areas you need and than put them in the condition range.您应该首先考虑您需要的所有区域,然后将它们放在条件范围内。

public String calculateGrade(int mark) {
        String grade = "";
if(mark < 0 || mark > 100){
            grade = "fail";
        } else {
            if (mark < 40) {
                grade = ("Fail"); 
            }else if (mark < 50) {
                grade = ("3rd");

            }else if (mark < 60) {
                grade = ("2ii");

            }else if (mark < 70) {
                grade = ("2i");

            }else if = (mark >= 70) {
                grade ("1st"); 
            }
        }
        return grade;


Always check else if in ascending order.如果按升序排列,请始终检查 else。

public String calculateGrade(int mark) {
        String grade = "";
        if (mark < 0) {
            return ("Invalid mark");
            }
        else if (mark < 40) {
            return ("Fail"); 
            }
        else if (mark < 50) {
            return ("3rd");
            }
        else if (mark < 60) {
            return ("2ii");
            }
        else if (mark < 70) {
            return ("2i");
            }
        else if (mark >= 70) {
            return ("1st"); 
            }
        else if (mark > 100) {
            return ("Invalid mark"); 
            }
        return grade;
    }

Now it should work as expected.现在它应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM