我遇到if if then statemenT的问题。我正在制作一个程序,告诉用户他是否输入一个整数,如果他得到100则为ex。这个程序的问题是如果我输入50,它将打印F和D标记。我觉得我的支架是罪魁祸首。如果你能提供帮助,那就太好了。
import java.util.Scanner;
public class LoanQualificationProgram{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int grade;
grade = keyboard.nextInt();
if (grade >=80)
System.out.println("Your grade is an A");
else {
if (grade >=70)
System.out.println("Your grade is B");
else {
if (grade>=60)
System.out.println("your grade is a C");
else {
if (grade >=50)
System.out.println("your grade is a D");
if (grade >=0)
System.out.println("your grade is an F");
}
}
}
答案 0 :(得分:4)
您不需要嵌套这些陈述。要有效地使用else-if
,请尝试以下方法:
if (grade >=80) {
// A...
}
else if (grade >=70) {
// B...
}
else if (grade>=60) {
// C...
}
// ...
else {
// Finally...
}
答案 1 :(得分:2)
试试这个兄弟或用例陈述:)
if (grade >=80)
System.out.println("Your grade is an A");
else if(grade >=70)
System.out.println("Your grade is B");
else if (grade>=60)
System.out.println("your grade is a C");
else if (grade >=50)
System.out.println("your grade is a D");
else if(grade >=0)
System.out.println("your grade is an F");