mercredi 23 mars 2016

Having trouble printing statement at the end of a for loop

I've been trying to get the last else if statement to execute but I've been unsuccessful any input would help

import java.util.Scanner; // Imports the Scanner class for keyboard input.
import java.util.Random; // Imports the Random class to generate a random number.

public class GuessingGameEC {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random ran = new Random(); 
        int randomNum = ran.nextInt(501); 
        int Guess = 0; 
        Scanner keyboard = new Scanner(System.in); 

        for (int numOfTries = 0; numOfTries <10; numOfTries++) {
        {
            System.out.println(" " );
            System.out.println("Guess what number I'm thinking of between 0 and 500.");
            Guess = keyboard.nextInt();


            if (Guess == randomNum) {
            System.out.println("You guessed the correct number, and it only took you " + numOfTries + " tries." );
            break;


            }

            else if (Guess < randomNum && Guess < 500 && Guess >= 0 && numOfTries != 10) {
            System.out.println("Your guess is too low, please try again!" );


            }

            else if (Guess > randomNum && Guess <= 500 && numOfTries != 10) {
            System.out.println("Your guess is too high, please try again!" );
            }

            else if (Guess > 500 || Guess < 0 ) {
                System.out.println("Please pick a number between 0 and 500!" );
            }


            else if (numOfTries == 10) {

                System.out.println(" " );
                System.out.println("Sorry, you're out of attempts. The correct number was " + randomNum +"!" );

            }
        }

      }

}
}

Aucun commentaire:

Enregistrer un commentaire