mercredi 3 juillet 2019

Why is the last else statement not printing/executing here?

The last else statement won't print anything... just takes the input and stops the program. What am I doing wrong?

I am completely new, just begun studying java. No prior experience in any code. Sorry.

Tried checking syntax everything looks ok to "me noob eyes"

import java.util.Scanner; public class MyClass { public static void main(String[] args)

{ 
    Scanner scn = new Scanner(System.in);

    int x =7;

    System.out.println("Enter a Number from 1 to 10: ");

    int guess = scn.nextInt();

    while(x != guess) 
    {
        if(x < guess) {
        System.out.println("Guess Lower...");
        guess = scn.nextInt();
        }

        else if (x > guess) {
            System.out.println("Guess Higher...");
            guess = scn.nextInt();
        }

        else {
        System.out.println("Correct Guess!");
        }
    } 
    }

}

So if i enter 7 it should say/show Correct Guess!

but it wont show anything.

Guess low and high works fine though.

Aucun commentaire:

Enregistrer un commentaire