Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA测验-重复相同的问题,而答案循环正确_Java - Fatal编程技术网

JAVA测验-重复相同的问题,而答案循环正确

JAVA测验-重复相同的问题,而答案循环正确,java,Java,我正在做一个由三个问题组成的测验。在选择了正确或错误的答案后,它应该转到下一个问题并给出下一组可能的答案 我的代码确实正确地显示了答案,但问题会重复三次。另一个副作用是,三个测试集变成九个,顺序如下:Q1/A1、Q1/A2、Q1/A3、Q2/A1、Q2/A2、Q2/A3、Q3/A1、Q3/A2、Q3/A3 期望的结果是Q1/A1、Q2/A2、Q3/A3 这是我的密码: import javax.swing.JOptionPane; 公开课测验{ 私有静态字符串应答=null; 私有静态字符串an

我正在做一个由三个问题组成的测验。在选择了正确或错误的答案后,它应该转到下一个问题并给出下一组可能的答案

我的代码确实正确地显示了答案,但问题会重复三次。另一个副作用是,三个测试集变成九个,顺序如下:Q1/A1、Q1/A2、Q1/A3、Q2/A1、Q2/A2、Q2/A3、Q3/A1、Q3/A2、Q3/A3

期望的结果是Q1/A1、Q2/A2、Q3/A3

这是我的密码:

import javax.swing.JOptionPane;
公开课测验{
私有静态字符串应答=null;
私有静态字符串answer2=null;
私有静态字符串answer3=null;
静态int-nQuestions=0;
静态整数nCorrect=0;
//主程序将调用测验中要回答的问题并检查其是否正确
//计算问题和正确答案的数量。
公共静态void main(字符串[]args){
String question=“什么是轨道最快的行星?\n”;
核对(问题、答案);
nQuestions=nQuestions+1;
showMessageDialog(null,“在“+nQuestions+”中有“+nCorrect+”正确答案”);
String question2=“2+2.0之和为?\n”;
核对(问题2,答案2);
nQuestions=nQuestions+1;
String Question 3=“球员将球射入篮中的运动是什么\n”;
核对(问题3,答案3);
nQuestions=nQuestions+1;
showMessageDialog(null,“在“+nQuestions+”中有“+nCorrect+”正确答案”);
}
//小测验#1
静态字符串询问(字符串问题){
问题+=“A.水星,旋转时间为58d 16小时。\n”;
问题+=“B.地球,旋转23小时56米。\n”;
问题+=“C.火星,旋转24小时36米。\n”;
问题+=“D.金星,旋转243d 26米。\n”;
问题+=“E.天王星,旋转17小时14米。\n”;
字符串answer=JOptionPane.showInputDialog(问题);
answer=answer.toUpperCase();
而(!answer.equals(“A”)和&!answer.equals(“B”)和&!answer.equals(“C”)和&!answer.equals(“D”)和&!answer.equals(“E”)){
showMessageDialog(null,“无效答案。请输入A、B、C、D或E.”;
answer=JOptionPane.showInputDialog(问题);
answer=answer.toUpperCase();
}
返回答案;
}
//测验2
静态字符串ask2(字符串问题2){
问题2+=“A.4\n”;
问题2+=“B.4.0\n”;
问题2+=“C.3\n”;
问题2+=“D.5\n”;
问题2+=“E-4\n”;
String answer2=JOptionPane.showInputDialog(问题2);
answer2=answer2.toUpperCase();
而(!answer2.equals(“A”)和&!answer2.equals(“B”)和&!answer2.equals(“C”)和&!answer2.equals(“D”)和&!answer2.equals(“E”)){
showMessageDialog(null,“无效答案。请输入A、B、C、D或E.”;
answer2=JOptionPane.showInputDialog(问题2);
answer2=answer2.toUpperCase();
}
回答2;
}
//小测验#3
静态字符串ask3(字符串问题3){
问题3+=“A.篮球。\n”;
问题3+=“B.排球。\n”;
问题3+=“C.棒球。\n”;
问题3+=“D.足球。\n”;
问题3+=“E.网球\n”;
String answer3=JOptionPane.showInputDialog(问题3);
answer3=answer3.toUpperCase();
而(!answer3.equals(“A”)和&!answer3.equals(“B”)和&!answer3.equals(“C”)和&!answer3.equals(“D”)和&!answer3.equals(“E”)){
showMessageDialog(null,“无效答案。请输入A、B、C、D或E.”;
answer3=JOptionPane.showInputDialog(问题3);
answer3=answer3.toUpperCase();
}
回答者3;
}
//应答验证器
静态无效检查(字符串问题、字符串答案){
字符串回答=提问(问题);
字符串correct=“D”;
如果(回答等于(正确)){
showMessageDialog(null,“Correct!”);
nCorrect=nCorrect+1;
}
else if(answer.equals(“A”)| answer.equals(“B”)| answer.equals(“C”)| answer.equals(“E”)){
showMessageDialog(null,“不正确!答案为”+正确);
}
//检查小测验#2
字符串回答2=ask2(问题);
字符串更正2=“B”;
如果(回答2.等于(更正2)){
showMessageDialog(null,“Correct!”);
nCorrect=nCorrect+1;
}
否则,如果(回答2.等于(“A”)|回答2.等于(“C”)|回答2.等于(“D”)|回答2.等于(“E”)){
showMessageDialog(null,“不正确!答案为”+correct2);
}
//检查小测验#3
字符串回答3=ask3(问题);
字符串correct3=“A”;
如果(回答3.等于(更正3)){
showMessageDialog(null,“Correct!”);
nCorrect=nCorrect+1;
}
否则如果(回答3.等于(B)|回答3.等于(C)|回答3.等于(D)|回答3.等于(E)){
showMessageDialog(null,“不正确!答案为”+correct3);
}       
}
}
我是一名java初学者,我确实意识到了某种意外的循环,但我无法修复它

有什么建议吗?提前感谢。

欢迎使用SO

您的代码有很多问题,但由于您刚刚开始,这是可以理解的

你应该考虑使用数组,这就是为什么我为你添加了一个数组,这样你就可以感觉到它们了。 您的主要问题是,您对每个问题使用相同的答案选择3次

下面的代码应该是
import javax.swing.JOptionPane;


public class Quiz {

    private static String answer = null;
    private static String answer2 = null;
    private static String answer3 = null;

    static int nQuestions = 0;
    static int nCorrect = 0;

    //Main program, will call the questions to be answered in the quiz and checks if it's correct
    //counting both the number of questions and correct answers.
    public static void main(String[] args) {

        String[] questions = {"What is the planet with fastest orbit?\n",
                "The sum of 2 + 2.0 is?\n",
                "What is the sport the player shoots the ball in a basket\n"
        };

        nQuestions = questions.length;

        checkQuestions(questions, answer3);

        JOptionPane.showMessageDialog(null,"Out of " + nQuestions + " you had "+ nCorrect + " correct answers");
    }


    //The quiz #1
    static String ask(String question) {

        question += "A. Mercury, with a rotation that takes 58d 16h.\n";

        question += "B. Earth, with a rotation that takes 23h 56m.\n";

        question += "C. Mars, with a rotation that takes 24h 36m.\n";

        question += "D. Venus, with a rotation that takes 243d 26m.\n";

        question += "E. Uranus, with a rotation that takes 17h 14m.\n";

        String answer = JOptionPane.showInputDialog(question);

        answer = answer.toUpperCase();

        while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D") && !answer.equals("E")) {

            JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
            answer = JOptionPane.showInputDialog(question);
            answer = answer.toUpperCase();

        }

        return answer;

    }


    //The quiz #2
    static String ask2(String question2) {

        question2 += "A. 4\n";

        question2 += "B. 4.0\n";

        question2 += "C. 3\n";

        question2 += "D. 5\n";

        question2 += "E. -4\n";

        String answer2 = JOptionPane.showInputDialog(question2);

        answer2 = answer2.toUpperCase();

        while (!answer2.equals("A") && !answer2.equals("B") && !answer2.equals("C") && !answer2.equals("D") && !answer2.equals("E")) {

            JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
            answer2 = JOptionPane.showInputDialog(question2);
            answer2 = answer2.toUpperCase();

        }

        return answer2;

    }


    //The quiz #3
    static String ask3(String question3) {

        question3 += "A. Basketball.\n";

        question3 += "B. Volleyball.\n";

        question3 += "C. Baseball.\n";

        question3 += "D. Football.\n";

        question3 += "E. Tennis\n";

        String answer3 = JOptionPane.showInputDialog(question3);

        answer3 = answer3.toUpperCase();

        while (!answer3.equals("A") && !answer3.equals("B") && !answer3.equals("C") && !answer3.equals("D") && !answer3.equals("E")) {

            JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");
            answer3 = JOptionPane.showInputDialog(question3);
            answer3 = answer3.toUpperCase();

        }

        return answer3;

    }


    //Answer verifier
    static void checkQuestions(String[] questions,  String correctAnswer) {
        String answer = ask(questions[0]);
        String correct = "D";
        if (answer.equals(correct)) {
            JOptionPane.showMessageDialog(null,"Correct!");
            nCorrect = nCorrect+1;
        }

        else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E")) {
            JOptionPane.showMessageDialog(null,"Incorrect! The answer is " + correct);
        }

        //checking Quiz#2
        String answer2 = ask2(questions[1]);
        String correct2 = "B";
        if (answer2.equals(correct2)) {
            JOptionPane.showMessageDialog(null,"Correct!");
            nCorrect = nCorrect+1;
        }

        else if (answer2.equals("A") || answer2.equals("C") || answer2.equals("D") || answer2.equals("E")) {
            JOptionPane.showMessageDialog(null,"Incorrect! The answer is " + correct2);

        }


        //checking Quiz#3
        String answer3 = ask3(questions[2]);
        String correct3 = "A";
        if (answer3.equals(correct3)) {
            JOptionPane.showMessageDialog(null,"Correct!");
            nCorrect = nCorrect+1;
        }

        else if (answer3.equals("B") || answer3.equals("C") || answer3.equals("D") || answer3.equals("E")) {
            JOptionPane.showMessageDialog(null,"Incorrect! The answer is " + correct3);

        }
    }
}