lundi 25 décembre 2017

While statement guessing game

Need help with this While statement. I can't for the life of me get line 19 to print. So my first question is how do I get line 19 to print. The second is, is the code skipping from line 19 to line 34 after too many wrong result. Is it possible to point me in the right direction without giving me the actual code. Thank You.

    import java.util.Scanner;

public class hiLow {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String playAgain = "";
        do {
            //Create a random number for the user to guess
            int theNumber = (int)(Math.random() * 100 + 1);
            System.out.println( theNumber );
            int guess = 0;
            int numberOfTries = 0;
            while (guess != theNumber && numberOfTries != 7) {
                numberOfTries++;
                System.out.println("Guess a number between 1 and 100");
                guess = scan.nextInt();
                if ((int) numberOfTries > 7) {
                    System.out.print("You Lose");
                    System.out.println("Would you like to play again (y/n)?");
                    playAgain = scan.next();
                while (playAgain.equalsIgnoreCase("y"));
                    System.out.println("Thank you for playing!  Goodbye.");}

                else if (guess < theNumber) {
                    System.out.println("Your entered " + guess + " is too low.  Try again.  " + numberOfTries);}
                else if (guess > theNumber) {
                    System.out.println("Your entered " + guess + " is too high.  Try again.  " + numberOfTries);}
                else {
                    System.out.println(guess + " is correct.  You win!!!");}
            }//end of while loop for guessing
            System.out.println("Would you like to play again (y/n)?");
            playAgain = scan.next();
        }while (playAgain.equalsIgnoreCase("y"));
        System.out.println("Thank you for playing!  Goodbye.");
        scan.close();


    }

}

Aucun commentaire:

Enregistrer un commentaire