用户在我的测验程序中选择了错误的答案之后如何重复这个问题

问题描述 投票:2回答:2

我想在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. 
java
2个回答
3
投票

我建议使用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年


0
投票

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

}

© www.soinside.com 2019 - 2024. All rights reserved.