Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to skip value if it is correct without asking input again?

I am instructed to skip a value if it has been guessed correctly so that it can proceed to ask for the other value without being repeated. I am only beginning to learn Java and have yet to learn how to do this. I tried to search up how to skip the correct guessed value but could not find anything. Is there a way to do this? Thank you in advance.

while (guessX != randomX || guessY != randomY) {
    System.out.print("Enter a guess for the X position of Mr. Yertle: ");
    guessX = scan.nextInt();
    if (guessX < randomX) {
        System.out.println("Too low! Guess higher.");
    } else if (guessX > randomX) {
        System.out.println("Too high! Guess lower.");
    } else {
        System.out.println("Ding, ding! You are correct!");
    }
    System.out.print("Enter a guess for the Y position of Mr. Yertle: ");
    guessY = scan.nextInt();
    if (guessY < randomY) {
        System.out.println("Too low! Guess higher.");
    } else if (guessY > randomY) {
        System.out.println("Too high! Guess lower.");
    } else {
        System.out.println("Ding, ding! You are correct!");
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Split them to 2 parts.

while (guessX != randomX) {
    System.out.print("Enter a guess for the X position of Mr. Yertle: ");
    guessX = scan.nextInt();
    if (guessX < randomX) {
        System.out.println("Too low! Guess higher.");
    } else if (guessX > randomX) {
        System.out.println("Too high! Guess lower.");
    } else {
        System.out.println("Ding, ding! You are correct!");
    }
}
while (guessY != randomY) {
    System.out.print("Enter a guess for the Y position of Mr. Yertle: ");
    guessY = scan.nextInt();
    if (guessY < randomY) {
        System.out.println("Too low! Guess higher.");
    } else if (guessY > randomY) {
        System.out.println("Too high! Guess lower.");
    } else {
        System.out.println("Ding, ding! You are correct!");
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading