在询问了指定数量的问题后,如何让我的程序停止提问?

时间:2018-03-29 00:47:45

标签: java

import java.util.Scanner;

public class MathPractice {

    public static void main(String[] args) {

        // Luke Mihalovich

        Scanner keyboard = new Scanner(System.in);

        int questions = getNumberOfQuestions();
        int difficulty = getQuestionDifficulty();

        while (questions > 0) {

        int random1 = (int)(9 * Math.random()) + 1;
        int random2 = (int)(9 * Math.random()) + 1;
        int randomSign = (int)(4 * Math.random()) + 1;
        int answerAdd;
        int rightAdd;
        int answerSub;
        int rightSub;
        int answerMul;
        int rightMul;
        int answerDiv;
        int rightDiv;

        if(randomSign == 1) {
            System.out.print("Question #" + questions + " What is " + random1 + " + " + random2 + "? ");
            answerAdd = keyboard.nextInt();
            rightAdd = random1 + random2;
            if (answerAdd == rightAdd) {
                System.out.println("Correct!");
            } else if (answerAdd != rightAdd) {
                System.out.print("Wrong... The answer is " + rightAdd);
            }
        }

        if(randomSign == 2) {
            System.out.print("Question #" + questions + " What is " + random1 + " - " + random2 + "? ");
            answerSub = keyboard.nextInt();
            rightSub = random1 - random2;
            if  (answerSub == rightSub) {
                System.out.print("Correct!");
            } else if (answerSub != rightSub) {
                System.out.print("Wrong... The answer is " + rightSub);
            }
        }

        if(randomSign == 3) {
            System.out.print("Question #" + questions + " What is " + random1 + " * " + random2 + "? ");
            answerMul = keyboard.nextInt();
            rightMul = random1 * random2;
            if  (answerMul == rightMul) {
                System.out.print("Correct!");
            } else if (answerMul != rightMul) {
                System.out.print("Wrong... The answer is " + rightMul);
            }
        }

        if(randomSign == 4) {
            System.out.print("Question #" + questions + " What is " + random1 + " / " + random2 + "? ");
            answerDiv = keyboard.nextInt();
            rightDiv = random1 / random2;
            if  (answerDiv == rightDiv) {
                System.out.print("Correct!");
            } else if (answerDiv != rightDiv) {
                System.out.print("Wrong... The answer is " + rightDiv);
            }
        }
        questions++;
        }   
    }   
        public static int getNumberOfQuestions() {

            Scanner keyboard = new Scanner(System.in);

            int questions = 0;

            System.out.print("How many questions do you want? ");
            questions = keyboard.nextInt();

            while(questions < 1) {
                System.out.println("The number of questions must be 1 or more.");
                System.out.print("How many questions do you want? ");
                questions = keyboard.nextInt();
            }
        return questions;
}
        public static int getQuestionDifficulty() {

            Scanner keyboard = new Scanner(System.in);

            int difficulty = 0;

            System.out.print("What difficulty level do you want (1=low or 2=high)? ");
            difficulty = keyboard.nextInt();

            while (difficulty > 1 | difficulty > 2) {
                System.out.println("Valid levels are 1 or 2. Please reenter. ");
                System.out.print("What difficulty level do you want (1=low or 2=high)? ");
                difficulty = keyboard.nextInt();
            }
        return difficulty;
        }
}

我的程序应该询问用户他们想要问的问题数量,然后该程序应该询问许多随机生成的数学问题。什么是我需要修复我的程序,以便在达到许多问题后停止提问?另外,我如何从#1开始提问?感谢

1 个答案:

答案 0 :(得分:1)

您可以使用for循环

替换

while (questions > 0) {

for (int loop = 1; loop <= questions; loop++) {

然后也替换像

这样的代码
System.out.print("Question #" + questions + " What is " + random1 + " + " + random2 + "? ");

System.out.print("Question #" + loop + " What is " + random1 + " + " + random2 + "? ");

另外,根据您的代码评论

 while (difficulty > 1 | difficulty > 2) {

错了,应该是

while (difficulty != 1 && difficulty != 2) {

修改

我注意到您的代码中有questions++; - 将其删除