否则如果声明不起作用(else if statement not working)

系统教程 行业动态 更新时间:2024-06-14 16:53:13
否则如果声明不起作用(else if statement not working)

我是Java新手,非常善于自学。 我正在练习else和if语句。 我已经编写了以下非常基本的程序来评分,但我不能得到所有其他if语句正常工作,它调用前两个我可以得到它显示A和B成绩但不是其余的,我有试图移动和移除波浪形支架等但仍然无法工作。 请帮助并原谅我的无知,因为我是Java编程的新手。

提前致谢。

public class Grade { public static void main(String[] arguments) { int grade = 69; if (grade > 90) { System.out.println("Well done you got a A"); } else if (grade < 90) { System.out.println("Well done you got a B"); } else if (grade < 85) { System.out.println("You got a C"); } else if (grade < 75) { System.out.println("You got a D"); } else if (grade < 75) { System.out.println("You got a E"); } else { System.out.println("You got a F"); } } }

I am new to Java and really kind of teaching myself. I am practising with the else and if statement. I have written the below very basic program for grading a score but i cannot get all of the else if statements to work properly, it invokes the first two and i can get it to display the A and B grades but not the rest, i have tried moving and removing the squiggly brackets and such but still wont work. Please help and excuse my ignorance as i am new to Java programming.

Thanks in advance.

public class Grade { public static void main(String[] arguments) { int grade = 69; if (grade > 90) { System.out.println("Well done you got a A"); } else if (grade < 90) { System.out.println("Well done you got a B"); } else if (grade < 85) { System.out.println("You got a C"); } else if (grade < 75) { System.out.println("You got a D"); } else if (grade < 75) { System.out.println("You got a E"); } else { System.out.println("You got a F"); } } }

最满意答案

将您的代码更改为:

public class Grade { public static void main(String[] arguments) { int grade = 69; if (grade >= 90) { System.out.println("Well done you got a A"); } else if (grade >= 85) { System.out.println("Well done you got a B"); } else if (grade >= 75) { System.out.println("You got a C"); } else if (grade >= 65) { System.out.println("You got a D"); } else if (grade >= 55) { System.out.println("You got a E"); } else { System.out.println("You got a F"); } } }

因为69 IS小于90所以第二个如果是真的那个人会得到一个B虽然他应该有D.如果第一个如果失败(这意味着等级低于90,那么你检查等级是否更大或等于85(然后得到B),检查它是否大于等于75等等

change your code to this:

public class Grade { public static void main(String[] arguments) { int grade = 69; if (grade >= 90) { System.out.println("Well done you got a A"); } else if (grade >= 85) { System.out.println("Well done you got a B"); } else if (grade >= 75) { System.out.println("You got a C"); } else if (grade >= 65) { System.out.println("You got a D"); } else if (grade >= 55) { System.out.println("You got a E"); } else { System.out.println("You got a F"); } } }

because 69 IS smaller than 90 so the second if will be true and the guy will get a B although he should have a D. If the first if fails (which means grade is lower than 90, then you check if the grade is greater or equal to 85 (then get B), orelse check if it is greater or equal than 75 et cetera

更多推荐

本文发布于:2023-04-06 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a24c320ed227ca9f377932d048725161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   声明   working   statement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!