简体   繁体   English

这是一个测验程序,我必须从 4 个选项中选择 1 个选项,然后我必须显示用户得到的错误和正确答案的数量

[英]this is a quiz program where i have to select 1 option from 4 and after I have to display how many wrong and right answer a user get

I don't know how to calculate how many is right and how many is wrong.我不知道如何计算多少是正确的,多少是错误的。

This is a quiz program where i have to select 1 option from 4 and after I have to display how many wrong and right answer a user gets...这是一个测验程序,我必须从 4 个选项中选择 1 个选项,然后我必须显示用户得到的错误和正确答案的数量……

My question is how I will calculate how many total wrong and right answer I got and percentage of total correct answer.我的问题是我将如何计算我得到的错误和正确答案总数以及正确答案总数的百分比。

I need to calculate it at the end.我需要在最后计算它。 I don't no how to calculate how many is right and how many is wrong.我不知道如何计算多少是正确的,多少是错误的。

//import scanner
import java.util.Scanner;

public class Quiz {

    //main method
    public static void main(String[] args) {
        //variable
        int choices;
        int song;
        int song2;
        int song3;
        int song4;
        int song5;

        //scanner for input options
        Scanner scan = new Scanner(System.in);
        System.out.println("Who sing this the song 'Locked Away':");

        //choose singer from these 4 options
        System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
        song = scan.nextInt();


        if (song != 1) {
            //if select wrong options
            System.out.println("Your answer is incorrect  R. City & Adam Levine's is the right answer .");
        }

        {
            while (song == 1) {
                //selected right option
                System.out.println("Congrate you answer is correct");

                song++;
            }
            System.out.println();
        }


        System.out.println("Who sing this the song 'Beauty and the beast':");

        //choose singer from these 4 options
        System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
        song2 = scan.nextInt();


        if (song2 != 2) {
            System.out.println("Your answer is incorrect  Justin Bieber is the right answer.");
        }

        {
            while (song2 == 2) {
                System.out.println("Congrate you answer is correct ");
                song2++;
            }
            System.out.println();
        }
        //name the singer of this song
        System.out.println("Who sing this the song 'Heal the world':");

        //choose singer from these 4 options
        System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Micheal Jackson.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
        song4 = scan.nextInt();


        if (song4 != 2) {
            System.out.println("Your answer is incorrect  Micheal Jackson is the right answer.");
        }

        {
            while (song4 == 2) {
                //display congrate message for right answer
                System.out.println("Congrate you answer is correct");
                song4++;
            }
            System.out.println();
        }

        //display Question
        System.out.println("Who sing this the song 'Roar':");

        //choose singer from these 4 options
        System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
        song3 = scan.nextInt();


        if (song3 != 4) {
            //display congrate message for incorrect answer
            System.out.println("Your answer is incorrect  Katy Perry is the right answer.");
        }

        {
            while (song3 == 4) {
                //display congrate message for right answer
                System.out.println("Congrate you answer is correct");
                song3++;
            }
            System.out.println();
        }
        //name of a singer
        System.out.println("Who sing this the song 'The Heart Wants What It Wants ':");

        //choose singer from these 4 options
        System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
        song5 = scan.nextInt();
        if (song5 != 3) {
            System.out.println("Your answer is incorrect  Salena Gomez is the right answer.");
        }
        {
            //using while loop
            while (song5 == 3) {
                //display congrats message for right answer
                System.out.println("Congrats you answer is correct");
                //stop repeating
                song5++;
            }  //stop repeating
            System.out.println();
        }
    }
}

I think this is what you are looking for我想这就是你要找的

import java.util.Scanner;


public class QuizProgram {

     //Initializaions and Instantiations
    static Scanner cin = new Scanner(System.in);

    public static void main(String[] args)
    {
           double score = 0;
           final double NumberofQuestions = 5;

          System.out.println("\n\n\n\n\n\n\n\t\tQuiz 1.0\n\n");

           //Store questions and answers in 2 dimensional array
           String[][] QandA = {
                               {"Who sing this the song 'Locked Away':?","\nChoose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.","1"},
                               {"Who sing this the song 'Beauty and the beast'?","\nChoose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.","2"},
                               {"Who sing this the song 'Heal the world'?","\nChoose 1 for R. City & Adam Levine's. \nChoose 2 for Micheal Jackson.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.","2"},
                               {"Who sing this the song 'Roar'?","\nChoose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.","4"},
                               {"Who sing this the song 'The Heart Wants What It Wants '?","\nChoose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.","3"} };

          String[] Answers = new String[(int) NumberofQuestions];

         //loop through each string in the array and compare it to answers
         //for(int x = 0; x < NumberofQuestions; x++)
          int x=0;
          do
         {
              System.out.print("" + (x+1) + ". " + QandA[x][0] + "   "+QandA[x][1]);

              Answers[x] = String.valueOf(cin.nextInt());

              Answers[x].toLowerCase();

              if(QandA[x][2].equals(Answers[x]))
              {
                   score++;
              }//close if 
              else
              {
                  System.out.println("\n Incorrect. The right answer is "+QandA[x][2]);
              }

              System.out.print("\n");
              x++;
         }while(x<NumberofQuestions);//close outer loop                                 

         System.out.println("\n\t\tYou got " + score + " of "
                            + NumberofQuestions + " right!\n\n\n"); 

         System.out.println("\n\t\tYour percentage is " + ((score/NumberofQuestions)*100)+"%"); 


         System.exit(0);

    }//close main() function  

//-------------------------------------------------------------------------------

}//close Quiz class

The general trend would be to keep two variables for right and wrong choices.总的趋势是为正确和错误的选择保留两个变量。 The structure should be like:结构应该是这样的:

   int correctAnswers = 0;
    int wronganswers = 0;
    //scanner for input options
    Scanner scan = new Scanner(System.in);
    System.out.println("Who sing this the song 'Locked Away':");
    //choose singer from these 4 options
    System.out.println("Choose 1 for R. City & Adam Levine's. \nChoose 2 for Justin Bieber.  \nChoose 3 for Selena Gomez. \nChoose 4 for Katy Perry.");
    song = scan.nextInt();
    if (song != 1) {
        //if select wrong options
        System.out.println("Your answer is incorrect  R. City & Adam Levine's is the right answer .");
        wronganswers++;
    } else {
        System.out.println("Congrate you answer is correct");
        song++;
        correctAnswers++;
    }

You can modify it as per your requirement.您可以根据您的要求对其进行修改。

I have used Dynamic memory allocation approach for this using arrayList.我使用arrayList为此使用了动态内存分配方法。 In the link I have made MCQ based question along with their options.在链接中,我提出了基于 MCQ 的问题及其选项。 see more 查看更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想计算我在测验应用程序中提交的每个正确和错误答案,并希望在 textview 中显示分数 - I want to count my every right and wrong answer submitted in my quiz app and want to display score in textview 用户在Java中的测验程序中选择了错误答案后如何重复问题 - How to repeat the question after the user chose the wrong answer on my quiz program in java 如何检查用户答案与测验中的代数答案相同? - How Do I Check If User Answer Is Same As Algebra Answer In Quiz? 如何让我的 Java 测验中的答案能够包含多个单词? - How do I make an answer able to have more than one word in my java quiz? 我对Euler#8项目的回答有误 - I have wrong answer for project Euler#8 如果答案是错误的android,我该如何显示正确的答案? - How can I show the right answer if the answer is wrong android? 如何创建新活动,我可以选择从数据库添加新项目? - How would I create new activity where I would have option to add new items from database? 我如何让用户从图库中选择多个图像,并在选择它们时将它们按顺序放入队列中? - How can I have a user select multiple images from the gallery, and as they are selected have them put into a queue in order? 已经尝试修复了很多次,我不知道我哪里出错了 - Have tried to fix this so many times I don't know where I'm going wrong 我正在为测验程序使用JTabbedPane,不确定如何获取按钮以显示答案 - I'm using JTabbedPane for my quiz program, not sure how to get my buttons to display the answers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM