简体   繁体   中英

making a simple math quiz with switch statement for loop and while loop in java

so my problem is that in my code I need it to ask 5 questions and then ask the user if he/she wants to continue or stop. If they choose to continue it should take them back to the menu(switch statement) and they can choose to do another set of math problems. for each option in the switch statement it should only have 5 math questions. If they choose stop the code should count how many questions it got right and calculate the percantage correct. Any tips or what is wrong with my code.. I know I am missing my for loop because I'm not quiet sure where to put it and how to make the code work with it in.

{
    //keyboard reader
    Scanner in = new Scanner((System.in));
    //3 number variables
    int num1;
    int num2;
    int answer;
    int operator;
    int question = 0;
    int num3;
    double questionCount = 0;
    double correct = 0;

    int i = 0;
    while (i < 7)
    {
        num1 = (int)(1+Math.random()*10);
        num2 = (int)(1+Math.random()*10);
        num3 = (int)(1+Math.random()*100);

        System.out.println("Welcome to the Wsu School of Math! ");

        System.out.println("please choose one of the following options for your math Quiz: ");
        System.out.println("1: Addition with number 1-10");
        System.out.println("2: Additon with numbers 1-100");
        System.out.println("3: Subtraction with numbers 1-10");
        System.out.println("4: Subtraction with numbers 1-100");
        System.out.println("5: Multipication with numbers 1-10");
        System.out.println("6: Exit the Quiz");
        operator = in.nextInt();
        switch (operator)
        {
            case 1: System.out.println(num1+"+"+num2+"=");
                question = num1 + num2;
                break;
            case 2: System.out.println(num1+"+"+num3+"=");
                question = num1 + num3;
                break;
            case 3: System.out.println(num1+"-"+num2+"=");
                if(num1 < num2)
                {
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                question = num1 - num2;
                break;
            case 4: System.out.println(num3+"-"+num1+"=");
                if(num1 > num3)
                {
                    int temp = num1;
                    num1 = num3;
                    num3 = temp;
                }
                question = num1 - num3;
                break;
            case 5: System.out.println(num1+"*"+num2+"=");
                question = num1 * num2;
                break;
            case 6:
                if(operator == 6)
                {
                    System.exit(-1);
                }
                break;
        }
        answer = Integer.parseInt(in.next());

        if(answer == -99)
        {
            System.out.print("good bye!\n");
        }
        else if (answer == question)
        {
            System.out.print("Correct!\n");
            questionCount++;
            correct++;
        }
        else
        {
            System.out.print("incorrect.\n");
            System.out.println("the correct answer is = "+question );
            questionCount++;
        }
    }
    System.out.println("Amount of problems attempted: "+questionCount);
    System.out.println("Amount of problems Correct: "+ correct);
    System.out.println("Percent you got on the test = "+correct/questionCount*100);
}

Try to write your "case 6" like this:

case 6:
        System.out.println("Amount of problems attempted: "+questionCount);
        System.out.println("Amount of problems Correct: "+ correct);
        System.out.println("Percent you got on the test = "+correct/questionCount*100); 

        System.exit(-1);
        break;

I cut the "System.out.print ..." from where they were and put them inside your "case 6:". But the System.exit(-1); will close the application. If you want to simply exit the while loop you would need a boolean to inter the loop and you would change its value inside "case 6" like this:

boolean a = true;
while(a){
    //...
   case 6:
       //...
       a = false;
       break;
   //...

}

or you can just make sure that your (i<7) become false. It depends on what you want to achieve. Hope this help!!!

Try as follows:

              case 6:
                  // DEL Start
                  //if(operator == 6)
                  //{
                  //    System.exit(-1);
                  //}
                  // DEL End

              break;


          }
          boolean isEnd = false;
          if (operator != 6) {  // Add
              answer = Integer.parseInt(in.next());
              // Add Start
              if (answer == question)
              {
                  System.out.print("Correct!\n");
                  questionCount++;
                  correct++;
              }
              else
              {
                  System.out.print("incorrect.\n");
                  System.out.println("the correct answer is = "+question );
                  questionCount++;
              }
              // Add End
          // Add Start
          }
          //
          else {
              //answer = -99;
              isEnd = true;
          }
          // Add End
          //if(answer == -99)
          if (isEnd)
          {
              System.out.print("good bye!\n");
              break;  // Add
          }
          // DEL Start
          //else if (answer == question)
          //{
          //    System.out.print("Correct!\n");
          //    questionCount++;
          //    correct++;
          //}
          //else
          //{
          //    System.out.print("incorrect.\n");
          //    System.out.println("the correct answer is = "+question );
          //    questionCount++;
          //}
          // DEL End
        }
        System.out.println("Amount of problems attempted: "+questionCount);
        System.out.println("Amount of problems Correct: "+ correct);
        System.out.println("Percent you got on the test = "+correct/questionCount*100);

From what i understood, you are missing a loop. The general structure of your code should look like this:

Scanner sc = new Scanner(System.in);
for (int questionBlock = 0; questionBlock < 5; questionBlock++) {
    System.out.println("select question..");
    int blockIndex = sc.nextInt();
    for (int question = 0; question < 5; question++) {
        //initialize random variables
        switch (blockIndex){
            case 1:
                //do stuff
                break;
            case 2:
                //do other stuff
                break;
            default:
                //wrong input.
        }
    }
    // evaluate results of last 5 questions..
}

When you are reading input, you might want to do it that way:

Scanner sc = new Scanner(System.in);
int blockIndex = 0;
while(blockIndex < 1 || blockIndex > 5) {
    System.out.println("Please select a question block, by entering it's number..");
    String text = sc.next();
    try{
        if (text.equals("exit")) {
            //exit
        } else {
            blockIndex = Integer.parseInt(text);
            continue;
        }
    }catch (NumberFormatException e) {
        //do nothing here
    }
    System.out.println(text+" was not a valid block number. Please enter an index between 1 and 5!");
}
//rest of code

This will make sure your input is between 1 and 5. also it gives the user the opportunity to exit the application by entering exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM