Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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,我想用java制作一个测验程序,我需要一个输出,如果用户选择了错误的答案,所有的问题都会重复,直到用户选择了正确的答案,并输入一个输出,该输出还必须显示更正的关键,我希望你们都能在我的java测验程序上提供帮助。我想更正我的分数,当5个错误的答案返回到有两个错误答案的问题时。当我运行代码时,分数为178分,代码中有5个错误答案,但当我完成测验时,分数为150分,这是我测验的总分 int score = 0; int count = 0; String

我想用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. 
int评分=0;
整数计数=0;
字符串名;
弦年龄;
字符串主题;
弦乐课程;
连续学年;
name=JOptionPane.showInputDialog(“输入您的姓名”);
age=JOptionPane.showInputDialog(“输入您的年龄”);
subject=JOptionPane.showInputDialog(“输入您的主题”);
course=JOptionPane.showInputDialog(“输入您的课程”);
学年=JOptionPane.showInputDialog(“今天输入您的学年”);
布尔值a=真;
布尔b=真;
布尔c=真;
做{
如果(a==true){
String question1=JOptionPane.showInputDialog(“Java发布的年份?\n”
+(“2分\n”)
+(“答案是B\n”)
+“(A)1990年\n(B)1991年\n(C)1998年\n(D)1980年\n”);
如果(“B”等于(问题1))
{
System.out.println(“正确的\n”);
分数+=2分;
计数++;
a=假;
}否则{
System.out.println(“不正确的\n”);
}
}       
如果(b==true){
String question2=JOptionPane.showInputDialog(“谁创建了Java?\n”
+(“2分\n”)
+(“答案是B\n”)
+“(A.)曼尼·帕奎奥(B.)詹姆斯·戈斯林(C.)詹姆斯·邦德(D.)马特·达蒙);
如果(“B”等于(问题2))
{
System.out.println(“正确的\n”);
分数+=2分;
计数++;
b=假;
}否则{
System.out.println(“不正确的\n”);
} 
}  
如果(c==true){
String question3=JOptionPane.showInputDialog(“Java是在哪个团队中创建的?\n”
+(“2分\n”)
+(“答案是D\n”)
+“(A.)黑色队\n(B.)白色队\n(C.)棕色队\n(D.)绿色队\n”);
如果(“D”等于(问题3))
{
System.out.println(“正确的\n”);
分数+=2分;
计数++;
c=假;
}否则{
System.out.println(“不正确的\n”);
}
}而(计数=150){
System.out.println(“优秀”);
}否则如果(分数>=140){
System.out.println(“最后通牒!”);
}否则如果(分数>=120){
System.out.println(“干得好!”);
}否则如果(分数>=100){
System.out.println(“干得好!”);
}否则如果(分数>=80){
系统输出打印项次(“通过”);
}否则如果(分数>=60){
System.out.println(“Passangawa”);
}否则如果(分数>=40){
系统输出打印项次(“满足”);
}否则如果(分数>=20){
System.out.println(“重试”);
}否则如果(分数>=0){
System.out.println(“失败”);
}否则{
System.out.println(“浪费!”);
```所有问题都将重复,直到用户选择了正确的答案,我需要分数将是正确的,当一个或多个错误的答案将返回到答案,分数将返回到正确的分数,共有28个问题。

我建议使用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");

                }
            }
        }


    }
}

MAIN

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

                }
            }
        }


    }
publicstaticvoidmain(字符串[]args){
智力得分=0;
问题q1=新问题(“Java发布的年份?\n”+(“2分\n”)
+“(A)1990年\n(B)1991年\n(C)1998年\n(D)1980年”;“B”);
问题q2=新问题(“hourse是什么”+(“2分”)+“(A.)动物\n(B.)狗\n(C.)人类\n(D.)湖\n”,
“A”);
列表问题列表=新的ArrayList();
问题列表添加(q1);
问题列表。添加(第2季度);
for(问题:问题列表){
布尔值isCorrectQuestion=false;
而(!isCorrectQuestion){
字符串answer=JOptionPane.showInputDialog(question.getQuestion());
if(question.getAnswer().equals(答案)){
System.out.println(“正确的\n”);
分数+=2分;
isCorrectQuestion=true;
}否则{
System.out.println(“不正确的\n”);
}
}
}
}
顺便说一下,Java的第一个版本是在1996年发布的

publicstaticvoidmain(String[]args){

int评分=0;
整数计数=0;
布尔值a=真;
布尔b=真;
做{
如果(a==true){
String question1=JOptionPane.showInputDialog(“Java发布的年份?\n”
+(“2分\n”)
+“(A)1990年\n(B)1991年\n(C)1998年\n(D)1980年\n”);
如果(“B”等于(问题1)){
System.out.println(“正确的\n”);
分数+=2分;
计数++;
a=假;
}否则{
System.out.println(“不正确的\n”);
}
}
如果(b==true){
String question2=JOptionPane.showInputDialog(“#2:Java发布的年份?\n”
+(“2分\n”)
+“(A)1990年\n(B)1991年\n(C)1998年\n(D)1980年\n”);
如果(“B”等于(问题2)){
System.out.println(“正确的\n”);
分数+=2分;
有限公司