【问题标题】: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这是一个测验程序,我必须从 4 中选择 1 个选项,然后我必须显示用户得到多少错误和正确答案
【发布时间】:2015-10-01 03:56:33
【问题描述】:

我不知道如何计算有多少是对的,有多少是错的。

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

我的问题是如何计算我得到的错误和正确答案总数以及正确答案总数的百分比。

我需要在最后计算它。我不知道有多少是对的,有多少是错的。

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

【问题讨论】:

    标签: java for-loop while-loop do-while


    【解决方案1】:

    我想这就是你要找的东西

    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
    

    【讨论】:

      【解决方案2】:

      总的趋势是为正确和错误的选择保留两个变量。结构应该是这样的:

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

      您可以根据自己的需要进行修改。

      【讨论】:

      • 你想在do-while循环中实现什么?退出该循环的条件是什么?
      【解决方案3】:

      我使用arrayList为此使用了动态内存分配方法。 在链接中,我提出了基于 MCQ 的问题以及他们的选项。 see more

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多