如何为乘法测验执行正确的循环

时间:2017-02-02 21:15:02

标签: java loops if-statement math infinite-loop

我是编程新手并想要一些帮助。

我试图创建一个无限循环的乘法测验,直到用户输入no。此外,当出现乘法问题时,程序还必须显示用户输入是否正确。

当用户输入no时,循环将停止并显示所提问题总数中的正确答案数。

任何想法?

这是我的代码:

public static void main(String[] args) {
    int correctCount = 0; // count the number correct question
    int count = 0; // count the number of question
    Scanner input = new Scanner(System.in);

   // 2 random single-digit int
    int number1 = (int) (Math.random() * 10);
    int number2 = (int) (Math.random() * 10);

   do {
     // prompt the student to answer "what is number1 - number2
     System.out.print("What is " + number1 + " * " + number2 + "?");
     int answer = input.nextInt();

     // grade the answer and display the result
     if (number1 * number2 == answer) {
        System.out.println("You are correct!");
        correctCount++; // increase the correct answer count
     } else {
        System.out.println("Your answer is worng. \n" + number1 + " * "
                + number2 + " should be: " + (number1 * number2));
     }

     count++;

     System.out.println("\nWould you like more questions?");
     System.out.print(" Y or N ?");
     String option = input.nextLine();
 } while (input.equals('y'));
    System.out.println("\nYou've answered " + correctCount + " out of "
        + count + " Correct");
}

我知道我的代码有点乱,因为我尝试执行do-while循环,但无法让我的程序正常运行。

3 个答案:

答案 0 :(得分:2)

你的主要问题是你正在检查input的值是否等于“y”,因为输入是扫描器而不是字符串。

为了完成这项工作,您需要进行一些更改。

首先你要选择do循环中的随机数,否则你会一遍又一遍地得到相同的数字。

其次,您希望将option字符串的声明移到循环之外,以便它具有可用的正确范围。

第三,你需要在获得input.nextLine() int之后调用answer,因为nextInt()方法只获取int,剩下的就是行。

第四,你想在下一行删除空格和换行符时使用.trim()

最后,当检查选项的值时,请使用.equalsIgnoreCase(),以便它适用于大写或小写y。

        int correctCount = 0; // count the number correct question
        int count = 0; // count the number of question
        Scanner input = new Scanner(System.in);



        String option;

        do {
            //2 random single-digit int
            int number1 = (int)(Math.random() * 10);
            int number2 = (int)(Math.random() * 10);  

            //prompt the student to answer "what is number1 - number2
            System.out.print("What is " + number1 + " * " + number2 + "?");
            int answer = input.nextInt();
            input.nextLine();

            //grade the answer and display the result
            if (number1 * number2 == answer)
            {
                System.out.println("You are correct!");
                correctCount++; //increase the correct answer count

            }
            else
            {
                System.out.println("Your answer is worng. \n" + 
            number1 + " * " + number2 + " should be: " + (number1 * number2));
            }

            count++;

            System.out.println("\nWould you like more questions?");
            System.out.print(" Y or N ?");

            option = input.nextLine().trim();
        } while (option.equalsIgnoreCase("Y"));

答案 1 :(得分:2)

您的do-while循环条件为while(input.equals('y')),但inputScanner对象,在此循环中无效。由于您已阅读用户的选择以继续option,因此应while(option.equals("y")),或更好,while(option.equalsIgnoreCase("y")),这将让用户输入yY

答案 2 :(得分:0)

我不完全确定扫描仪出了什么问题,但我更新了你的课程,现在工作正常。我在循环中移动了随机数,因此每次迭代的数字都会改变。我还将while条件更改为“input.next()。equalsIgnoreCase(”y“)”。当它是input.nextLine()。equalsIgnoreCase(“y”)时,它会在一次迭代时停止。我不熟悉Scanner类的奇怪之处,并且只会使用Reader。

public class Main {
    public static void main(String[] args) {
        int correctCount = 0; // count the number correct question
        int count = 0; // count the number of question
        Scanner input = new Scanner(System.in);

        do {
            //2 random single-digit int
            int number1 = (int) (Math.random() * 10);
            int number2 = (int) (Math.random() * 10);

            //prompt the student to answer "what is number1 - number2
            System.out.print("What is " + number1 + " * " + number2 + "?");
            int answer = input.nextInt();

            //grade the answer and display the result
            if (number1 * number2 == answer) {
                System.out.println("You are correct!");
                correctCount++; //increase the correct answer count

            } else {
                System.out.println("Your answer is worng. \n" +
                        number1 + " * " + number2 + " should be: " + (number1 * number2));
            }

            count++;

            System.out.println("\nWould you like more questions?");
            System.out.print(" Y or N ?");
        } while (input.next().equalsIgnoreCase("y"));


        System.out.println("\nYou've answered " + correctCount + " out of " + count
                + " Correct");
    }
}