使用switch语句for循环和while循环在java中进行简单的数学测验

时间:2016-02-06 22:00:37

标签: java

所以我的问题是在我的代码中我需要它询问5个问题,然后询问用户他/她是否想要继续或停止。如果他们选择继续它,应该将它们带回菜单(switch语句),他们可以选择另外一组数学问题。对于switch语句中的每个选项,它应该只有5个数学问题。如果他们选择停止,代码应该计算它正确的问题,并计算正确的percantage。任何提示或我的代码有什么问题..我知道我错过了我的for循环,因为我不确定在哪里放它以及如何使代码与它一起使用。

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

3 个答案:

答案 0 :(得分:4)

尝试写下你的“案例6”:

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;

我从他们所在的地方剪下了“System.out.print ...”并将它们放在你的“案例6:”中。但是System.exit(-1);将关闭该应用程序。如果你想简单地退出while循环,你需要一个布尔值来循环,你会在“case 6”中改变它的值,如下所示:

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

}

或者你可以确保你的(i <7)变得虚假。这取决于你想要达到的目标。希望这有帮助!!!

答案 1 :(得分:0)

尝试如下:

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

答案 2 :(得分:0)

据我所知,你错过了一个循环。代码的一般结构应如下所示:

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..
}

当您阅读输入时,您可能希望这样做:

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

这将确保您的输入介于1和5之间。此外,通过输入exit

,用户可以退出应用程序。