简体   繁体   中英

infinite Do loop in a body of the method

Could you please help to get my head around why Do statement in the body of the method keeps looping infinitely?

I've created a class Guesser . The input is taken from a user as an integer within the main() method and determined answer as an integer determined within main() method as well.

The method itself is designed to validate guessed parameter inputted by the user against the determined answer (which is 5) and return the output to console either "you're wrong..." or "Correct!".

So, whenever I insert 5 it works alright with one problem it looks like the inputted value is passed on to the method twice. And it makes a problem as whenever I input 4 the result is correctly validated and the output generated in the console returns the right statement but then the value is passed on and on again returning getting caught in the loop infinitely returning the same statement.

Here is the code:

import java.util.Scanner;
//class begins here
class Guesser {
int answer1;
int guess;

//constructor
Guesser(int ans, int gs) {
    answer1 = ans;
    guess = gs;
}

//Method starts here
void method1() {
//Do control statement comes here
do {
    System.out.println("Guess the number...");
    if(this.guess != this.answer1) {
        System.out.print("Your guess is worng. You're too ");
        if(this.guess < this.answer1) System.out.println("low");
        else System.out.println("high");
    } //end of if statement
} while (this.guess != this.answer1); //end of Do Control Statement
System.out.println("Correct!");
} //end of method1
} //End of the class Guesser

//Main class comes here
public class DemoGuess {

    public static void main(String args[]) {
        System.out.println("Guess the number...");
        int input;

        Scanner in = new Scanner(System.in);
        input = in.nextInt();
        Guesser ActionGuess = new Guesser(5,input);
        ActionGuess.method1();


    } //end of main() method

} //end of DemoGuess class

At no point in your loop do you wait for the user to enter a number, and store that number in this.guess . Rather, you keep looping until their original guess becomes correct. Which, of course, never happens.

You need to prompt for and read input during the loop to get new guess values.

public void method1() {
    Scanner input = new Scanner(System.in);
    int guess = 0;
    do {
        System.out.println("Guess the number...");
        guess = input.nextInt(); // prompt user for a new guess
        if (guess != answer) {
            System.out.print("Your guess is wrong.\nYou are too ");
            System.out.println((guess < answer) ? "low" : "high");
        }
    } while (guess != answer);
    System.out.println("Correct!");
    input.close();
}

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