Java 简单测验获胜';Don’不要等待答复
作为在线练习的一部分,我一直在尝试做一个简单的测验。我有两个问题,其中一个if语句应该只接受三种可能结果中的一种,正确答案、错误答案或错误选择 我的问题是,当用户做出错误的选择(如A B或C)并按下Q时,程序会跳过下一个问题,而不等待正确或错误的答案。据我所知,除非满足条件,否则if语句不应允许程序运行,但无论如何,错误的选择仍会转移到下一个问题 请在你的回答中残忍一些,我感觉我学到的方法有点过时了Java 简单测验获胜';Don’不要等待答复,java,if-statement,Java,If Statement,作为在线练习的一部分,我一直在尝试做一个简单的测验。我有两个问题,其中一个if语句应该只接受三种可能结果中的一种,正确答案、错误答案或错误选择 我的问题是,当用户做出错误的选择(如A B或C)并按下Q时,程序会跳过下一个问题,而不等待正确或错误的答案。据我所知,除非满足条件,否则if语句不应允许程序运行,但无论如何,错误的选择仍会转移到下一个问题 请在你的回答中残忍一些,我感觉我学到的方法有点过时了 import java.util.Scanner; class quiz { publ
import java.util.Scanner;
class quiz {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a;
String b;
String c;
String name;
int score = 0;
System.out.println("Welcome to your new quiz !");
System.out.println("What is your name?");
name = scan.nextLine();
System.out.println("Welcome " + name + " let's start the quiz !");
System.out.println("What is Ireland's capitol city?");
System.out.println("Is it\n\t\tA)Dublin\n\t\tB)Galway\n\t\tC)Cork\n");
System.out.println("Please select A B or C");
System.out.println("Your current score is " + score);
String q;
q = scan.nextLine();
if (q.toLowerCase().equals("a")) {
System.out.println("Correct!");
score = score + 1;
System.out.println("Your new score is " + score);
} else if (q.toLowerCase().equals("b")) {
System.out.println("Sorry that was incorrect!");
} else if (q.toLowerCase().equals("c")) {
System.out.println("Sorry that was incorrect!");
} else {
System.out.println("You pressed " + q + " Please choose A B or C");
}
System.out.println("Second Question !!");
System.out.println("What was the currency unit for Ireland before the Euro was introduced?");
System.out.println("Is it\n\t\tA)Pound\n\t\tB)Dollar\n\t\tC)Punt\n");
System.out.println("Please select A B or C");
System.out.println("Your current score is " + score);
String w;
w = scan.nextLine();
if (w.toLowerCase().equals("c")) {
System.out.println("Correct!");
score = score + 1;
System.out.println("Your new score is " + score);
} else if (w.toLowerCase().equals("b")) {
System.out.println("Sorry that was incorrect!");
} else if (w.toLowerCase().equals("a")) {
System.out.println("Sorry that was incorrect!");
} else {
System.out.println("You pressed " + w + " Please choose A B or C");
}
}
}
您需要在程序中实现一个循环,这样它将不断地询问答案,直到输入有效的响应为止。while循环可以实现这一点,如下所示: 定义一个布尔(真/假)变量,用于跟踪是否做出了有效响应:
boolean validResponse = false;
然后在while循环中使用此选项,在必要时反复提问:
while (!validResponse) {
q = scan.nextLine();
if (q.toLowerCase().equals("a")) {
System.out.println("Correct!");
score++;
System.out.println("Your new score is " + score);
validResponse = true;
}
// else if 'b', if 'c', etc.
if (!validResponse) {
System.out.println("You pressed " +q+" Please choose A B or C");
}
}
while循环中的代码将重复运行,直到测试失败-在这种情况下,当validResponse
设置为true时。此时,程序将继续执行循环后面的任何操作
有更好的方法来实现这个程序,但我尽量少修改代码。例如,您可能希望将问题和答案存储在数据结构中,并对其进行迭代,而不是对每个问题重复几乎相同的代码块。正如我在评论中所说,这里需要一个带有布尔标志的循环。
将
While
条件放置在Do
循环的底部将确保循环中的代码至少运行一次。
如果将条件放置在顶部,如下所示:
while (guessed){
// your code goes here
}
boolean correctAnswer = false;
do {
w = scan.nextLine();
if (w.toLowerCase().equals("c")){
score++; //same as score = score + 1;
correctAnswer = true;
System.out.println("Correct! Your new score is " +score);
}else if (w.toLowerCase().equals("b")){
System.out.println("Sorry that was incorrect!");
}else if (w.toLowerCase().equals("a")){
System.out.println("Sorry that was incorrect!");
}else{
System.out.println("You pressed " +w+" Please choose A B or C");
}
} while (!correctAnswer);
然后,如果条件已经满足,代码块将不会执行
System.out.println("Second Question !!");
System.out.println("What was the currency unit for Ireland before the Euro was introduced?");
System.out.println("Is it\n\t\tA)Pound\n\t\tB)Dollar\n\t\tC)Punt\n");
System.out.println("Please select A B or C");
System.out.println("Your current score is " + score);
String w;
boolean guessed = false;
do{
w = scan.nextLine();
if (w.toLowerCase().equals("c")) {
System.out.println("Correct!");
score = score + 1;
System.out.println("Your new score is " + score);
guessed = true;
} else if (w.toLowerCase().equals("b")) {
System.out.println("Sorry that was incorrect!");
guessed = true;
} else if (w.toLowerCase().equals("a")) {
System.out.println("Sorry that was incorrect!");
guessed = true;
} else {
System.out.println("You pressed " + w + " Please choose A B or C");
}
}while (!guessed);
您的代码将如下所示:
while (guessed){
// your code goes here
}
boolean correctAnswer = false;
do {
w = scan.nextLine();
if (w.toLowerCase().equals("c")){
score++; //same as score = score + 1;
correctAnswer = true;
System.out.println("Correct! Your new score is " +score);
}else if (w.toLowerCase().equals("b")){
System.out.println("Sorry that was incorrect!");
}else if (w.toLowerCase().equals("a")){
System.out.println("Sorry that was incorrect!");
}else{
System.out.println("You pressed " +w+" Please choose A B or C");
}
} while (!correctAnswer);
我现在知道,条件3,选择错误,已经得到满足,程序正在进行。我想知道如何强制它等待条件1或2。我建议您查看
Do While
循环以实现这一点。这是一个很好的答案,非常感谢。我希望困惑的阴霾最终会消散,我能开始擅长这些东西。感谢您花时间帮助一位新手。请注意-您需要将w=scan.nextLine()。。。对吧?:)呵呵,有点。。。你学的越多,你想解决的问题就越复杂,但这很有趣:)谢谢Micheal,不是每天我都能得到这么多支持,谢谢你帮助新来的家伙:)