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 +"!" );
}
}
}
}
}
As your for loop goes from 0 to 9, the case of numOfTries == 10 will never happen.
I would change your code so that you return when sucessful
if (Guess == randomNum) {
System.out.println("You guessed the correct number, and it only took you " + numOfTries + " tries." );
return;
}
and then after your loop have finished, you can unconditionally print that you failed
Your for loop conditional terminates when numOfTries >= 10. You should put your last block of code outside the for loop.
Related
import java.util.Random;
import java.util.Scanner;
public class Activity3 {
public static void main(String[] args) {
//Variables
Scanner input = new Scanner(System.in);
Random Machine = new Random();
int num = Machine.nextInt(10);
do {
System.out.println("Guess the random generated number of the machine from 1-10");
int guess = input.nextInt();
if (guess == num) {
System.out.println("Correct number= " + num);
System.out.println("You Win!");
} else if (guess <= 0 && guess >= 11) {
System.out.println("Invalid Number!");
}
if (guess > 1 && guess < 10){
System.out.println("You Lose:<");
}
System.out.println("Do you want to try again?");
} while (input.next().equalsIgnoreCase("YES"));
input.close();
}
}
If I guess the correct number it outputs " you win!".
If I guess wrong it outputs "you lose". But If I guess a number that isn't in 1-10 it doesn't output the "Invalid Number" and just proceeds to output the "Do you want to try again?".
Random#nextInt(int) will return a value from 0 to bound - 1, so it's possible that the guess could be 0 in your code. You'd correct this by adding 1 to the guess, for example int num = Machine.nextInt(10) + 1;
Look at your logic...
else if(guess <= 0 && guess >= 11) {
if guess <= 0 AND guess >= 11 ... well, that's impossible.
I would change your logic flow, focusing on "happy paths" first.
That is, is the input within the acceptable range? If so, is guess == num if so, you win, otherwise print error messages.
For example...
Scanner input = new Scanner(System.in);
Random Machine = new Random();
int num = Machine.nextInt(10) + 1;
boolean done = false;
do {
System.out.println("Guess the random generated number of the machine from 1-10");
// Read the WHOLE line of text, removing the new line from the
// buffer which would otherwise be left by Scanner#nextInt
// and would cause no end of issues
String text = input.nextLine();
try {
// Try and parse the text to an int
int guess = Integer.parseInt(text);
if (guess >= 1 && guess <= 10) {
if (guess == num) {
System.out.println("Correct number= " + num);
System.out.println("You Win!");
num = Machine.nextInt(10) + 1;
System.out.println("Would you like to play another game? (Yes/No)");
} else {
System.out.println("Incorrect, guess again");
System.out.println("Do you want to try again? (Yes/No)");
}
// Prompt the user to try again or play another game
text = input.nextLine();
done = !"yes".equals(text.toLowerCase());
} else {
System.out.println("Out of range");
}
} catch (NumberFormatException exp) {
System.out.println("Not a valid number");
}
} while (!done);
I'm fairly new to java and I was wondering how could I reset this game to ask another number after the user guessed it correctly?
Here's my code so far:
import java.util.Scanner;
public class Question2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("Welcome to the Number Guessing Game");
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess < 0 || guess > 100){
if(guess == -1){
System.out.print("Thank you for playing the game!");
break;
}
System.out.print("Out of bounds. Try Again: ");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
}
}
}
Just wrap your entire code (except for the scanner initialization) in a while loop that is always true. That way, when one game ends, it will start a new one. Then, instead of breaking your game's while loop when the user enters a -1, just use System.exit(0), which will end your program with a status code of 0, indicating that the program executed successfully.
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
while (true) {
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("Welcome to the Number Guessing Game");
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess < 0 || guess > 100) {
if (guess == -1) {
System.out.print("Thank you for playing the game!");
System.exit(0);
}
System.out.print("Out of bounds. Try Again: ");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
} else if (guess < a) {
System.out.print("The number is higher. Try again: ");
} else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
}
}
}
Wrap your code in a while (true) this will keep on running your code for ever and ever. Make sure you are also updating your random a after every game and your count. Then from there just check if guess is ever -1 and return when it is. When you call return it will end the method which ends your game.
Scanner keyboard = new Scanner(System.in);
while (true){
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("Welcome to the Number Guessing Game");
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess==-1){
System.out.print("Thank you for playing the game!");
return;
}else if (guess < 0 || guess > 100){
System.out.print("Out of bounds. Try Again: ");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
}
}
You need to:
move your end game condition in the while condition like this while(guess != -1)
move the welcome greetings inside the loop
move the thank you greeting after the game loop is done
reset count and a when the user won a game in order to start fresh
reset guess on every iteration
Now even if the player guesses the number, the loop does not end and the game loop can be stopped only intentionally (by entering -1 = current break condition):
import java.util.Scanner;
public class Question2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("Welcome to the Number Guessing Game");
while (guess != -1) {
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
guess = keyboard.nextInt();
count++;
if (guess < 0 || guess > 100){
System.out.print("Out of bounds. Try Again: ");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
a = 1 + (int) (Math.random() * 99);
System.out.println("Congratulations. You guessed the number in " + count + " tries!");
count = 0;
}
guess = 0;
}
System.out.print("Thank you for playing the game!");
}
The code can be refactored even more, for example extract functionality into functions in order to make the code more readable. This also leads to easier maintanence should the variables change or should more conditions come. For example the code can be refactored like this:
import java.util.Scanner;
public class Question2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int a = 0;
int count = 0;
int guess = 0;
startNewGame();
System.out.println("Welcome to the Number Guessing Game");
while (guess != -1) {
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
guess = keyboard.nextInt();
count++;
if (guess < 0 || guess > 100){
System.out.print("Out of bounds. Try Again: ");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in " + count + " tries!");
startNewGame();
}
resetGuess();
}
System.out.print("Thank you for playing the game!");
}
private static int generateNewA() {
return 1 + (int) (Math.random() * 99);
}
private static void startNewGame() {
a = generateNewA();
count = 0;
}
private static void resetGuess() {
guess = 0;
}
}
Another solution is to use two nested loops, but IMO for this case loop in a loop is too much and makes the source unnecessary complex.
I'm kinda new to java and I was wondering how to loop this code until someone enters -1 and how to add "out of bounds" if the user enters number not between 0-100?
here's my code so far:
import java.util.Scanner;
public class Question2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int count = 0;
int a = 1 + (int) (Math.random() * 99);
int guess = 0;
System.out.println("Welcome to the Number Guessing Game");
System.out.print("Guess a number between 0 and 100 or enter -1 to end: ");
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
}
}
}
You can check for the bounds within the for loop. In order to stop further checks, you can iterate to the next loop through the continue call. Otherwise, if you actually want to break out of the loop, use the break call (for the -1 case).
while (guess != a) {
guess = keyboard.nextInt();
count++;
if (guess < 0 || guess > 100){
if(guess == -1)
break;
System.out.println("Out of bounds");
continue;
}
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
System.out.println("Thank-you for playing the game!!");
You want to do some code while something is not = to -1. In java, we use
!=
to represent not equal. So, try this code with the do while in mind:
do{
guess = keyboard.nextInt();
count++;
if (guess > a) {
System.out.print("The number is lower. Try again: ");
}
else if (guess < a) {
System.out.print("The number is higher. Try again: ");
}
else if (guess == a) {
System.out.println("Congratulations. You guessed the number in "
+ count + " tries!");
}
}
}
\\Here comes the **while**
} while (guess != a && keyboard.in != -1)
The while states that when guess is not a AND the user has not entered -1. This is the only way that the do can be executed.
Thats it! I suggest you read more about math operations and loops.
Best of luck,
{Rich}
I can't make this program show the message "Number out of range" when the user put a number out of (1 - 6). I tried different ways of code, but I don't know what I am doing wrong.
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && (guess <7 || guess>0))) {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}if(guess >=7 || guess<=0 && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
} if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
You need to position the condition to check if the input is a valid input or not at the start of the while loop , currently, it will only be checked after all tries are exhausted as you end the while loop before it.
Also, you should change the while loop condition from (tries >1 && (guess <7 || guess>0)) to (tries > 1 && guess != diceNumber) , since you may not want to exit the loop if the number entered is outside the range.
Code -
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && guess != diceNumber)) {
if((guess >=7 || guess<=0) && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
}
else {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}
}
if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
The below code will fetch you the expected result.
import java.util.Random;
import java.util.Scanner;
public class Dice {
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6) + 1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have "
+ tries + " tries.");
guess = input.nextInt();
loop: while ((tries >= 1)) {
--tries;
if ((guess >= 7 || guess <= 0) && (tries >= 1)) {
System.out.println("Number out of range, try again; you have "
+ tries + " more tries.");
guess = input.nextInt();
} else {
if (guess == diceNumber) {
System.out.println("You Win!!");
break loop;
} else if (tries >= 1) {
System.out.println("Incorrect Number, you have " + tries
+ " more tries.");
guess = input.nextInt();
}
}
}
if (tries == 0 && guess != diceNumber) {
System.out.println("You Lose!!");
}
}
}
I think the problem is in your while loop.
I'd say it should be something like this:
while(tries > 0 && guess != diceNumber) {
tries--;
if (guess >= 1 && guess <= 6) {
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
} else if (guess >=7 || guess<=0) {
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
}
if (tries > 0) {
guess = input.nextInt();
}
}
if(tries == 0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
I think this should do what you want.
As you can see, I changed the while condition:
You want the user to be able to enter another number while he has at least 1 try left, so tries>0.
You want the user to enter another number only if guess != diceNumber.
The if conditions inside also change:
You only say it's an incorrect number if the entered number is between 1 and 6: guess >= 1 && guess <= 6.
You only say it's out of range if the number is not between 1 and 6: guess >= 7 || guess <= 0.
You don't need to check the number of tries, the while loop does that already.
You only want to get a new input number if the user as a try left: tries > 0.
The program will generate the correct output for the first number guess but after the user inputs the second guess, there is no output at all. Please help! THANKS
final int number = (int)((Math.random()*99)+1);
int counter = 0;
System.out.print("Enter a guess between 1 and 100: ");
while (keyboard.nextInt() > number) {
System.out.println("Your guess was too high. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() < number) {
System.out.println("Your guess was too low. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() == number) {
System.out.println("Congratulations! Your guess was correct!");
counter++;
System.out.println("I had chosen " + number + " as the target number.");
System.out.println("You guessed it in " + counter + " tries.");
if (counter == 1) {
System.out.println("That was lucky!");
}
if (counter >= 2 || counter <= 4) {
System.out.println("That was amazing!");
}
if (counter == 5 || counter == 6) {
System.out.println("That was good.");
}
if (counter == 7) {
System.out.println("That was OK.");
}
if (counter == 8 || counter == 9) {
System.out.println("That was not very good.");
}
if (counter >= 10) {
System.out.println("This just isn't your game.");
}
you really need to change your design pattern.
Once you fail to satisfy the conditions in 1 of those while loops, you code will never go back.
you should only have 1 while loop for the guessing phase. you code should look like this
while(someCondition)
{
int num = keyboard.nextInt()
if (num > number) {
...
}
else if (num < number) {
...
}
else if (num == number) {
...
}
}
Your while loops should be if statements.
There should be a while loop around (practically) all your code
You code should roughly look like:
while(true) {
// prompt for input
// read input
// break from loop if input is the exit input, eg -1
// check input - essentially change your whiles to ifs
}
This block doesn't seem to work as intended:
while (keyboard.nextInt() > number) {
System.out.println("Your guess was too high. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
while (keyboard.nextInt() < number) {
System.out.println("Your guess was too low. Try again.");
counter++;
System.out.print("Enter a guess between 1 and 100: ");
}
Assume the user enters -1 and then 101. You'll end up in the next loop. Thus you might want to change it to something like this:
boolean retry = true;
while ( retry ) {
counter++;
int n = keyboard.nextInt();
if( n > number ) {
System.out.println("Your guess was too high. Try again.");
}
else if( n < number ) {
System.out.println("Your guess was too low. Try again.");
}
else {
//number found
retry = false;
}
if( retry ) {
System.out.prnt("Enter a guess between 1 and 100: ");
}
}
There is some problem in how you use the while statement.
Here you can find the correct version of your code:
public static void main(final String[] args) {
final Scanner keyboard = new Scanner(System.in);
final int number = (int) ((Math.random() * 99) + 1);
int counter = 0;
System.out.print("Enter a guess between 1 and 100: ");
int user_number;
do {
user_number = keyboard.nextInt();
if (user_number > number) {
System.out.println("Your guess was too high. Try again.");
System.out.print("Enter a guess between 1 and 100: ");
counter++;
} else if (user_number < number) {
System.out.println("Your guess was too low. Try again.");
System.out.print("Enter a guess between 1 and 100: ");
counter++;
}
} while (user_number != number);
System.out.println("Congratulations! Your guess was correct!");
counter++;
System.out.println("I had chosen " + number + " as the target number.");
System.out.println("You guessed it in " + counter + " tries.");
if (counter == 1) {
System.out.println("That was lucky!");
}
if ((counter >= 2) || (counter <= 4)) {
System.out.println("That was amazing!");
}
if ((counter == 5) || (counter == 6)) {
System.out.println("That was good.");
}
if (counter == 7) {
System.out.println("That was OK.");
}
if ((counter == 8) || (counter == 9)) {
System.out.println("That was not very good.");
}
if (counter >= 10) {
System.out.println("This just isn't your game.");
}
}
while(someCondition)
{
if (keyboard.nextInt() > number) {
...
} else if(keyboard.nextInt() < number) {
...
} else {
...
}
}