使用循环进行多项选择和真/假测验

时间:2016-11-24 22:36:45

标签: java loops while-loop

我正在尝试为必须有5个选择题和5个真/假问题的用户构建测验。我必须使用循环(while循环)。我已经开始设置一个单独的方法,询问用户问题和错误检查的真/假或多选问题。如果他们正确回答每个问题,我现在必须以某种方式给用户一个观点。然后最后,我必须给用户他们赢得的总积分。然后我不得不问他们是否想在最后再玩一次,如果他们说是的话我必须回到第一个问题并重新开始游戏,如果他们说没有该程序必须关闭。这是我的主要方法的地方。我开始为第一个答案设置一个while循环(正确的答案是3)并制作一个点变量,但我不知道从那里去哪里以及如何连接所有东西。我希望到目前为止所做的是正确的。谢谢!

    UserInteraction input = new UserInteraction();
    Questions ask = new Questions();

    int answer1 = 0, answer2 = 0, answer3 = 0, answer4 = 0, answer5 = 0; 

    int a1 = ask.Question1(answer1);
    int point;

    while (a1==3)
    {point = 1;

    }

    int a2 = ask.Question2(answer2);
    int a3 = ask.Question3(answer3);
    int a4 = ask.Question4(answer4);
    int a5 = ask.Question5(answer5);


    boolean answer6=false, answer7=false, answer8=false, answer9=false, answer10=false;

    String a6 = ask.Question6(answer6);
    String a7 = ask.Question7(answer7);
    String a8 = ask.Question8(answer8);
    String a9 = ask.Question9(answer9);
    String a10 = ask.Question10(answer10);

对于Questions方法,我会在这里放两个空白的例子。

{public int Question1 (int answer1)
    {String message = "";
    int smallestValue = 1;
    int largestValue = 4;

    System.out.println("Q1) What is...?");
    System.out.println("1: ....");
    System.out.println("2: ......");
    System.out.println("3: ......");
    System.out.println("4: ......");
    System.out.print("Enter the number");

    Scanner input = new Scanner(System.in);
    UserInteraction input2 = new UserInteraction();

    answer1 = input2.getIntValueFromUserBetween(message, smallestValue, largestValue);
    return answer1;

}

public String Question6(boolean answer10)
    {String message = "";

    System.out.println("(Q10) ....(true/false)");
    System.out.print("Enter your answer here: ");

    Scanner input = new Scanner(System.in);
    UserInteraction input2 = new UserInteraction();

    answer10 = input2.confirm(message);

    return "" + answer10;

}
}

2 个答案:

答案 0 :(得分:1)

很抱歉,如果我误解了你的问题,但我不明白为什么你在这里使用循环。

while (a1==3)

您的程序要么被困在这里,要么永远不会使用它。我的意思是,如果用户正确回答问题(即3),他们将被困在while循环中,直到你设置a1!= 3.

我认为更好的解决方案是使用选择。例如:

if (a1 == 3) {
     point += 1; // point = point + 1
     // Or whatever functionality you need here
}

编辑:如果你真的必须使用一个循环,那么有一个布尔标志将是要走的路。例如:

Boolean flag = false;    

if (a1 == 3) {
    flag = true;
    while (flag) {
        point += 1; //point = point + 1
        // Make sure that you set flag equals to false at the end of the loop though, otherwise it will infinitely loop
        // Include any other functionality needed
        flag = false;
    }
}

答案 1 :(得分:0)

这与你想要的相似吗?

int correct;
public void quiz() { // this is so you can restart quiz easily
    String[] answers = String[5];
    //add answers to array, set them to variables/constant first then index
    String[] questions = String[5];
     // add questions to array
    for(int i = 0; i <= questions.length; i++) { // stops after all questions have been asked, make sure its "<="
        System.out.println(questions[i]); // prints question 1 first loop then 2 and so on
        // read input from user
        if(input == answers[i]) { // you may have to convert input to correct type
            correct += 1;
 }
}
}
 System.out.println("You got " + correct + " correct answers");
 System.out.println("Would you like to play again?");
      if(input == yes) {
           quiz(); //starts quiz again // starts quiz method again

P.S。对不起,如果我误解了这个问题

要问不同的问题,您只需更改字符串变量,然后调用quiz()来询问这些问题。好又简单:))