Java 使用循环的多项选择题和正确/错误测验

Java 使用循环的多项选择题和正确/错误测验,java,loops,while-loop,Java,Loops,While Loop,我正在尝试为一个用户建立一个测验,它必须有5个选择题和5个正确/错误的问题。我必须使用循环来完成这项工作。我已经到了这样一个地步,我建立了一个单独的方法,询问用户问题,并对真/假或多项选择题进行错误检查。如果用户正确回答了每个问题,我现在不得不给他们一分。最后,我必须给用户他们赢得的总积分。然后我要问他们是否想在最后再玩一次,如果他们说是,我必须回到第一个问题,重新开始游戏,如果他们说不是,程序必须关闭。这就是我的主要方法。我开始为第一个答案设置一个while循环,正确答案为3,并生成一个点变量

我正在尝试为一个用户建立一个测验,它必须有5个选择题和5个正确/错误的问题。我必须使用循环来完成这项工作。我已经到了这样一个地步,我建立了一个单独的方法,询问用户问题,并对真/假或多项选择题进行错误检查。如果用户正确回答了每个问题,我现在不得不给他们一分。最后,我必须给用户他们赢得的总积分。然后我要问他们是否想在最后再玩一次,如果他们说是,我必须回到第一个问题,重新开始游戏,如果他们说不是,程序必须关闭。这就是我的主要方法。我开始为第一个答案设置一个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);
对于提问方法,我将在这里放置两个空白示例

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

}
}

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

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;
    }
}
编辑:如果您真的必须使用循环,那么使用布尔标志就是最好的选择。例如:

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

这和你想要的相似吗

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
如果我误解了这个问题,我很抱歉


要问不同的问题,您只需更改字符串变量,然后调用测验来问这些问题。很好也很简单:

问题方法的代码?是的,我知道我没有完成那部分,所以我只是在上面放了一些开始的东西,并设置了循环。我希望有一种方法可以使用循环,而不仅仅是使用条件句。你想要使用循环的原因是什么?我真的不明白。但这是可能的。我会更新我的答案。好的,如果用户想再次玩,我可能需要一个循环来返回问题。我将尝试使用这两种方法,看看效果如何。谢谢在这种情况下,您可以在while循环中完成整个过程。类似于我使用布尔标志的方式。布尔标志=真;虽然标志{//在这里实现代码//如果continue==N{flag=false;}}检查用户是否希望继续,只是为了查看您的情况?你能让它工作吗?我也很抱歉懒惰,没有添加输入代码和作业等。我已经为你评论了这一切!但这很容易,所以你不需要我把它放在汉克!我现在开始工作了。太好了,一开始你似乎对第一个答案感到困惑,所以我想我应该把这个作为第二选择。很高兴你成功了!