英文:
How to use while loop to ask for response until a correct response is given
问题
我正在创建一个计算器,根据你的等级来计算你获得的分数,但我想要使用一个while循环,如果输入不是一个实际的等级,代码将持续询问,直到输入一个实际的等级为止。但是当我加入while循环时,扫描器似乎根本不扫描输入?(对Java相当新,尝试实现类似问题的答案,但仍然不起作用)
double firsth2 = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("你的最高分H2科目是什么等级?");
String firstgrade = scanner.next();
while (!(firstgrade.equalsIgnoreCase("A") || firstgrade.equalsIgnoreCase("B") || firstgrade.equalsIgnoreCase("C") || firstgrade.equalsIgnoreCase("D") || firstgrade.equalsIgnoreCase("E") || firstgrade.equalsIgnoreCase("S") || firstgrade.equalsIgnoreCase("U"))) {
System.out.println("请输入一个实际的等级");
scanner.reset();
firstgrade = scanner.next();
}
switch (firstgrade) {
case "A", "a" -> firsth2 = 20;
case "B", "b" -> firsth2 = 17.5;
case "C", "c" -> firsth2 = 15;
case "D", "d" -> firsth2 = 12.5;
case "E", "e" -> firsth2 = 10;
case "S", "s" -> firsth2 = 5;
case "U", "u" -> firsth2 = 0;
}
英文:
I am creating a calculator to calculate the number of points you get based on your grade but i want to make a while loop such that if the response is not an actual grade, the code will keep asking till an actual grade is given. but when I add the while loop, the scanner does not seem to scan the input at all? (quite new to java, tried to implement the answers from similar questions but still doesn't work)
double firsth2 = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("What did you get for your highest scoring H2 subject?");
String firstgrade = scanner.next();
while (!(firstgrade.equalsIgnoreCase("A") && firstgrade.equalsIgnoreCase("B") && firstgrade.equalsIgnoreCase("C") && firstgrade.equalsIgnoreCase("D") && firstgrade.equalsIgnoreCase("E") && firstgrade.equalsIgnoreCase("S") && firstgrade.equalsIgnoreCase("U"))) {
System.out.println("Please enter a real grade");
scanner.reset();
firstgrade= scanner.next();
}
switch (firstgrade) {
case "A", "a" -> firsth2 = 20;
case "B", "b" -> firsth2 = 17.5;
case "C", "c" -> firsth2 = 15;
case "D", "d" -> firsth2 = 12.5;
case "E", "e" -> firsth2 = 10;
case "S", "s" -> firsth2 = 5;
case "U", "u" -> firsth2 = 0;
}
答案1
得分: 1
你可以这样做:
final Scanner userInput = new Scanner(System.in);
final String ls = System.lineSeparator();
double firstH2 = 0d;
String firstGrade = "";
while (firstGrade.isEmpty()) {
System.out.print("你的最高分H2科目是什么?"
+ ls + "输入等级:A,B,C,D,E,S,U,或Q退出:-> ");
firstGrade = userInput.nextLine();
switch (firstGrade.toUpperCase()) {
case "A": firstH2 = 20; break;
case "B": firstH2 = 17.5; break;
case "C": firstH2 = 15; break;
case "D": firstH2 = 12.5; break;
case "E": firstH2 = 10; break;
case "Q":
System.out.println("退出 - 再见");
return;
case "S": firstH2 = 5; break;
case "U": firstH2 = 0; break;
default: {
System.out.println("无效等级 (" + firstGrade + ")!请重试..." + ls);
firstGrade = "";
}
}
}
System.out.println(firstH2);
英文:
You could do it this way:
final Scanner userInput = new Scanner(System.in);
final String ls = System.lineSeparator();
double firstH2 = 0d;
String firstGrade = "";
while (firstGrade.isEmpty()) {
System.out.print("What did you get for your highest scoring H2 subject?"
+ ls + "Enter Grade: A, B, C, D, E, S, U, or Q to exit: -> ");
firstGrade = userInput.nextLine();
switch (firstGrade.toUpperCase()) {
case "A": firstH2 = 20; break;
case "B": firstH2 = 17.5; break;
case "C": firstH2 = 15; break;
case "D": firstH2 = 12.5; break;
case "E": firstH2 = 10; break;
case "Q":
System.out.println("Quiting - Bye Bye");
return;
case "S": firstH2 = 5; break;
case "U": firstH2 = 0; break;
default: {
System.out.println("Invalid Grade (" + firstGrade + ") Supplied! Try again..." + ls);
firstGrade = "";
}
}
}
System.out.println(firstH2);
答案2
得分: 0
尝试使用scanner.nextLine
而不是scanner.next
。
英文:
Try using scanner.nextLine
instead of scanner.next
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论