简单的测验不会等待答案

时间:2015-06-16 22:57:42

标签: java if-statement

我一直在努力做一个简单的测验作为在线练习的一部分,我有2个问题,if语句应该只接受3种可能结果中的1种,正确的答案,错误的答案或错误的选择。< / p>

我的问题是,当用户做出错误的选择,例如A B或C并且用户按下Q时,程序跳到下一个问题而不等待正确或错误的答案。据我所知,if语句不应该允许程序流动,除非满足条件,但不管选择是否仍然是错误的选择移动到下一个问题。

请回答你的残酷,我觉得我学到的方法已经过时了。

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");
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您需要在程序中实现循环,以便在输入有效响应之前不断询问答案。 while循环可以实现这一点,如下所示:

定义一个布尔(true / false)变量,用于跟踪是否已做出有效响应:

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时。此时,程序将继续循环后的任何内容。

有更好的方法来实现此程序,但我尝试尽可能少地更改代码。例如,您可能希望将问题和答案存储在数据结构中,并对其进行迭代,而不是为每个问题重复几乎相同的代码块。

答案 1 :(得分:1)

正如我在评论中所说,这里需要一个带有布尔标志的循环 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

While条件放在Do循环的底部将确保循环中的代码至少运行一次。 如果您将条件放在顶部,如下所示:

while (guessed){
    // your code goes here
}

然后如果条件已经满足,则不会执行代码块。

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);

答案 2 :(得分:1)

您的代码将如下所示:

    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);