Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

oop - I wrote a java program but it seems to be an infinite loop

import java.util.Scanner;

public class FirstJava {
    public static void main(String args[]) {
        final int NUMBER_OF_QUESTIONS = 5;
        int correctCount = 0;
        int count = 0;

        Scanner input = new Scanner(System.in);

        while (count < NUMBER_OF_QUESTIONS) {
            int number1 = (int) (Math.random() * 10);
            int number2 = (int) (Math.random() * 10);

            if (number1 < number2) {
                int temp = number1;
                number1 = number2;
                number2 = temp;
            }

            System.out.print("What is " + number1 + " - " + number2 + "? ");
            int answer = input.nextInt();

            while (number1 - number2 != answer) {
                System.out.println("Wrong, try again.");
                System.out.println("What is " + number1 + " - " + number2 + "? ");
            }

            System.out.println("Correct!");
            count++;
            correctCount++;

        }
        System.out.println("You got " + correctCount + " correct!");
    }
}

I think there is something wrong with the second while loop but I can't figure out what. The goal is to ask subtraction question until the user gets it right. Can someone help me with this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that the inner loop never returns once entered since the condition never changes. You can ask for the user input directly inside the condition:

System.out.print("What is " + number1 + " - " + number2 + "? ");

while (input.nextInt() != number1 - number2) {
    System.out.println("Wrong, try again.");
    System.out.print("What is " + number1 + " - " + number2 + "? ");
}

A few side-notes for the rest of the code:

  • Instead of (int) (Math.random() * 10), I suggest to use Random#nextInt().
  • The outer while-loop can be replaced by an easier to read for-loop.
  • Use camelCase for the variable NUMBER_OF_QUESTIONS or move it to the class as a static member.
  • Use Java notation (String[] args) for the argument array of the main method instead of C-style (String args[]).
  • By the way... correctCount will always be the same and might be removed if not used.

Here is the adapted class with above suggestions:

import java.util.Random;
import java.util.Scanner;

public class SubtractionQuiz {

    private static final int NUMBER_OF_QUESTIONS = 5;

    public static void main(String[] args) {
        final Scanner input = new Scanner(System.in);
        final Random random = new Random();

        int correctCount = 0;
        for (int count = 0; count < NUMBER_OF_QUESTIONS; count++) {
            int number1 = random.nextInt(10);
            int number2 = random.nextInt(10);

            // order numbers
            if (number1 < number2) {
                int temp = number1;
                number1 = number2;
                number2 = temp;
            }

            System.out.print("What is " + number1 + " - " + number2 + "? ");
            while (input.nextInt() != number1 - number2) {
                System.out.println("Wrong, try again.");
                System.out.print("What is " + number1 + " - " + number2 + "? ");
            }
            System.out.println("Correct!");
            correctCount++;
        }

        System.out.println("You got " + correctCount + " correct!");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...