我是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
更多推荐
发布评论