Reputation: 25
I'm doing a program in java that needs me have the user guess the number with only 10 tries and also use a while loop. This is all I currently have, can anyone help as to where to put the while loop and what I'm missing? Thank you.
import java.util.Scanner;
import java.util.Random;
public class GuessGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Random generator = new Random();
Scanner input = new Scanner(System.in);
int numberToGuess = 1 + generator.nextInt(100);
int numberOfTries = 0;
int limit= 10;
int guess;
System.out.println ("You have 10 tries to guess a number between 1 and 100");
System.out.print("Guess number 1: ");
guess= input.nextInt();
numberOfTries++;
if (guess < numberToGuess)
System.out.println("Your guess is too low. Try again.");
else if (guess > 100)
System.out.println("Guesses should be between 1 and 100.");
else if (guess > numberToGuess)
System.out.println("Too high. Try again.");
else if (guess == numberToGuess)
System.out.println("Congratulations!");
System.out.print("Guess number 2: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 3: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 4: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 5: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 6: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 7: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 8: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 9: ");
guess= input.nextInt();
numberOfTries++;
System.out.print("Guess number 10: ");
guess= input.nextInt();
numberOfTries++;
}
}
Upvotes: 1
Views: 1306
Reputation: 31968
user guess the number with only 10 tries
while(numberOfTries<10){
System.out.print("Guess number " + (numberOfTries+1) + ": ");
guess= input.nextInt();
if (guess < numberToGuess)
System.out.println("Your guess is too low. Try again.");
else if (guess > 100)
System.out.println("Guesses should be between 1 and 100.");
else if (guess > numberToGuess)
System.out.println("Too high. Try again.");
else if (guess == numberToGuess){
System.out.println("Congratulations!");
break;
}
numberOfTries++;
}
// If after executing the while loop the numberOfTries is equal to 10, that means the user had made all attempt.
if(numberOfTries == 10){
System.out.println("Sorry, you did not guess the guess the answer in 10 tries");
System.out.println("The number was" +numberToGuess);
}
This replaces all your statements post
System.out.println ("You have 10 tries to guess a number between 1 and 100");
Upvotes: 3
Reputation: 1097
Try something like this:
Random generator = new Random();
Scanner input = new Scanner(System.in);
int numberToGuess = 1 + generator.nextInt(100);
int numberOfTries = 0;
int limit= 10;
int guess;
System.out.println ("You have 10 tries to guess a number between 1 and 100");
while(numberOfTries < 10){
System.out.print("Guess number " + numberOfTries + ": ");
guess= input.nextInt();
if (guess < numberToGuess)
System.out.println("Your guess is too low. Try again.");
else if (guess > 100)
System.out.println("Guesses should be between 1 and 100.");
else if (guess > numberToGuess)
System.out.println("Too high. Try again.");
else if (guess == numberToGuess){
System.out.println("Congratulations!");
break;
}
numberOfTries++;
}
Upvotes: 2
Reputation: 1974
Instead of taking 10 numbers as separate statements, do something like this :
int guess = 0;
guess = input.nextInt();
while( guess != numberToGuess || numberOfTrys > 10){
System.out.println("Enter a new guess :: ");
guess = input.nextInt();
numberOfTrys ++;
}
Our condition checks if our guess is correct or the number of trys has exceeded ten. if either of these two conditions are true, it exits the while loop where you can further handle the programs logic.
Upvotes: 0