簡體   English   中英

用戶在Java中的測驗程序中選擇了錯誤答案后如何重復問題

[英]How to repeat the question after the user chose the wrong answer on my quiz program in java

我想用Java編寫測驗程序,我需要一個輸出,如果用戶選擇了錯誤的答案,所有問題都會重復執行,直到用戶選擇正確的答案,然后輸出還必須顯示更正鍵的輸出,希望您我的Java測驗程序中的所有幫助人員。 而且我想在5個錯誤的答案又有2個錯誤答案的情況下更正我的分數。 當我運行代碼時,分數是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. 

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

公共靜態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);
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM