它不允许我输入我的选择是否要进入另一个年级,它应该允许我选择是或否,如果我输入是,它应该允许我进入另一个年级,除了它只是拒绝接受用户输入?
import java.util.Scanner;
public class ExaminationGrades {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
double grade;
String ch;
do {
System.out.println("Enter your grade: ");
grade = scan.nextDouble();
if (grade <40) {
System.out.println("you failed");
} else if (grade >=40 && grade <50) {
System.out.println("Pass");
} else if (grade >=50 && grade <60) {
System.out.println("2:2");
} else if (grade >=60 && grade <70){
System.out.println("2:1");
} else if (grade >=70 && grade <=100){
System.out.println("1:1");
} else
System.out.println("invalid entry");
System.out.println("Would you like to enter another?");
ch = scan.nextLine();
} while(ch =="no");
scan.close();
}
}
答案 0 :(得分:0)
更改
while(ch =="no");
到
while(ch.equals("yes"))
修改强>
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
double grade;
String ch;
do {
System.out.println("Enter your grade: ");
grade = scan.nextDouble();
scan.nextLine(); //Consume the newline character generated by hitting enter
if (grade <40) {
System.out.println("you failed");
}else if (grade >=40 && grade <50) {
System.out.println("Pass");
}else if (grade >=50 && grade <60) {
System.out.println("2:2");
}else if (grade >=60 && grade <70){
System.out.println("2:1");
}else if (grade >=70 && grade <=100){
System.out.println("1:1");
}else
System.out.println("invalid entry");
System.out.println("Would you like to enter another?");
ch = scan.nextLine();
}while(ch.equals("yes")); //Correctly compare strings
scan.close();
}