如何重复的问题后,用户选择了错误的答案对我的测验程序在java

tez616oj  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(199)

我想用java做一个测验程序,我需要一个输出,如果用户选择了错误的答案,所有的问题将重复,直到用户选择了正确的答案,并把输出,必须显示也关键纠正,我希望你们都能帮助我的测试程序在java的家伙。我想纠正我的分数时,5错误的答案将回到两个错误的答案的问题。当我运行代码时,得分是178分,其中代码中有5个错误答案,但当我完善测验时,得分是150分,这是我测验的总分

int score = 0;
           int count = 0;

        String name;
        String age;
        String subject;
        String course;
        String schoolyear;
        name = JOptionPane.showInputDialog("Enter your name");
        age = JOptionPane.showInputDialog("Enter your age");
        subject = JOptionPane.showInputDialog("Enter your subject");
        course = JOptionPane.showInputDialog("Enter your course");
        schoolyear = JOptionPane.showInputDialog("Enter your school year today");


        boolean a = true;
        boolean b = true;
        boolean c = true;

 do {
        if (a == true) {
        String question1 =JOptionPane.showInputDialog("What year when the release of Java?\n"
                         + ("2 points \n")
                         + ("The answer is B\n")
                         + "(A.)1990\n (B.)1991\n (C.)1998\n (D.)1980\n" );


    if("B".equals(question1)) 
    {
        System.out.println("Correct\n");
        score+= 2;
                count++;
                a = false;

    }else{
        System.out.println("Incorrect\n");

    }

 }       

        if (b == true) {
        String question2 =JOptionPane.showInputDialog("Who created Java?\n"
                         + ("2 points \n")
                         + ("The answer is B\n")
                         + "(A.)Manny Pacquio\n (B.)James Gosling\n (C.)James Bond\n (D.)Matt Damon\n");

    if("B".equals(question2)) 

    {
            System.out.println("Correct\n");
        score+= 2;
                count++;
                b = false;

    }else{
        System.out.println("Incorrect\n");

    } 

    }  

        if (c == true) {
        String question3 =JOptionPane.showInputDialog("In Which team where Java created?\n"
                         + ("2 points \n")
                         + ("The answer is D\n")
                         + "(A.)Team Black\n (B.)Team White\n (C.)Team Brown\n (D.)Team Green\n" );

    if("D".equals(question3))
    {
        System.out.println("Correct\n");
        score+= 2;
                count++;
                c = false;

    }else{
        System.out.println("Incorrect\n");
    }
} while (count <3);     

   } 

Scanner input = new Scanner(System.in);
       System.out.println("Your score: " + score);
       System.out.println(100 *score/150 + "%");

       if (score >=150) {
       System.out.println("Excellent");
       } else if (score >=140) {
       System.out.println("Ultimatum!");
       } else if (score >=120) {
       System.out.println("Great Job!");
       } else if (score >=100) {
       System.out.println("Good Job!");
       } else if (score >=80) {
       System.out.println("Pass");
       } else if (score >=60) {
       System.out.println("Passangawa");
       } else if (score >=40) {
       System.out.println("Satisfy");
       } else if (score >=20) {
       System.out.println("Try again");
       } else if (score >=0) {
       System.out.println("Failed");    
       } else {
       System.out.println("Wasted!");  


```All question will repeat until the user chose the correct answer and i need the score will be correct when one or more wrong answer will back to answer again and the score will back to its correct score with the 28 question in total.
eqqqjvef

eqqqjvef1#

我建议使用while循环,如果答案是正确的,则添加一些标志,并迭代,直到答案正确为止

    • 问题**
public class Question {
private String question;
private String answer;

public Question(String question, String answer) {
    this.question = question;
    this.answer = answer;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public String getAnswer() {
    return answer;
}

public void setAnswer(String answer) {
    this.answer = answer;
}

}

    • 主要**
public static void main(String[] args) {
        int score = 0;
        Question q1 = new Question("What year when the release of Java?\n" + ("2 points \n")
                + "(A.)1990\n (B.)1991\n (C.)1998\n (D.)1980\n", "B");
        Question q2 = new Question("What is hourse" + ("2 points \n") + "(A.)Animal\n (B.)Dog\n (C.)Human\n (D.)Lake\n",
                "A");

        List<Question> questionsList = new ArrayList<>();
        questionsList.add(q1);
        questionsList.add(q2);

        for(Question question : questionsList) {
            boolean isCorrectQuestion = false;
            while (!isCorrectQuestion) {
                String answer = JOptionPane.showInputDialog(question.getQuestion());
                if (question.getAnswer().equals(answer)) {
                    System.out.println("Correct\n");
                    score += 2;
                    isCorrectQuestion = true;
                } else {
                    System.out.println("Incorrect\n");

                }
            }
        }

    }

顺便说一下Java的第一个版本是在1996年

ogsagwnx

ogsagwnx2#

public static void main(String[] args) {

        int score = 0;
        int count = 0;

        boolean a = true;
        boolean b = true;

        do {
            if (a == true) {
                String question1 = JOptionPane.showInputDialog("What year when the release of Java?\n"
                        + ("2 points \n")
                        + "(A.)1990\n (B.)1991\n (C.)1998\n (D.)1980\n");

                if ("B".equals(question1)) {
                    System.out.println("Correct\n");
                    score += 2;
                    count++;
                    a = false;
                } else {
                    System.out.println("Incorrect\n");
                }

            }

            if (b == true) {
                String question2 = JOptionPane.showInputDialog("#2: What year when the release of Java?\n"
                        + ("2 points \n")
                        + "(A.)1990\n (B.)1991\n (C.)1998\n (D.)1980\n");

                if ("B".equals(question2)) {
                    System.out.println("Correct\n");
                    score += 2;
                    count++;
                    b = false;
                } else {
                    System.out.println("Incorrect\n");
                }

            }

        } while (count < 2);
    }
}

相关问题