Jump to content

trouble with random number game

- - - - -

  • Please log in to reply
3 replies to this topic

#1
bchav

bchav

    Newbie

  • Members
  • Pip
  • 4 posts
Hi, I'm a pain in the ass newbie, I'm trying to write this guessing game that makes them guess untill they have guessed all 3 numbers correctly.

I have a print of the correct numbers at top just for testing.

The other caveat that is killing me is that if the user enters the letter 'n' at anytime the program should exit but I have NO idea how to do that when user input is and int and not a char. :(

I’m having trouble getting this simple program to work, and to make it worse user input of ‘n’ is supposed to exit the program but I don’t know how I can do that when expected input is an integer and not a char.


int userGuess1, userGuess2, userGuess3;
int secretNum1, secretNum2, secretNum3;

secretNum1 = (int) (Math.random() * 3 + 1);
secretNum2 = (int) (Math.random() * 3 + 1);
secretNum3 = (int) (Math.random() * 3 + 1);

System.out.println(secretNum1 + " " + secretNum2 + " " + secretNum3);

System.out.println("This is the number guessing game!" +
"\nPlease enter three numbers from 1 to 3.");

System.out.println("You may input an 'n' to quit the program at any time.");

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter number 1: ");
userGuess1 = keyboard.nextInt();

System.out.println("Please enter number 2: ");
userGuess2 = keyboard.nextInt();

System.out.println("Please enter number 3: ");
userGuess3 = keyboard.nextInt();

do{
if(userGuess2 == secretNum2 && userGuess3 == secretNum3){
System.out.println("Number 2 and 3 are correct.");
System.out.println("Please enter number 1: ");
userGuess1 = keyboard.nextInt();}}
while(userGuess1 != secretNum1);

do{
if(userGuess1 == secretNum1 && userGuess3 == secretNum3){
System.out.println("Number 1 and 3 are correct.");
System.out.println("Please enter number 2: ");
userGuess2 = keyboard.nextInt();}}
while(userGuess2 != secretNum2);

do{
if(userGuess1 != secretNum1 && userGuess2 == secretNum2){
System.out.println("Number 1 and 2 are correct.");
System.out.println("Please enter number 3: ");
userGuess3 = keyboard.nextInt();}}
while(userGuess3 != secretNum3);

if(userGuess1 == secretNum1 && userGuess2 == secretNum2 && userGuess3 == secretNum3){
System.out.println("CONGRATUALTIONS, ALL THREE NUMBERS WERE GUESSED CORRECTLY!");
}
}
:confused:

#2
Directive

Directive

    Newbie

  • Members
  • Pip
  • 5 posts
Since you are storing the numbers in an int type reference, if the user inputs an 'n' an InputMismatchException will be thrown because 'n' is not an integer. You can catch the exception thrown and handle it by exiting the program.

Hope this helps a bit! :)

#3
bchav

bchav

    Newbie

  • Members
  • Pip
  • 4 posts
Use a try and catch??

Thanks for the reply!

---------- Post added at 11:46 PM ---------- Previous post was at 10:08 PM ----------

just wanted to follow up I ended up doing this.. its kinda clunky but it works.. lol

int userGuess1, userGuess2, userGuess3;
int secretNum1, secretNum2, secretNum3;

secretNum1 = (int) (Math.random() * 3 + 1);
secretNum2 = (int) (Math.random() * 3 + 1);
secretNum3 = (int) (Math.random() * 3 + 1);

System.out.println("This is the number guessing game!" +
"\nPlease enter three numbers from 1 to 3.");

System.out.println("You may input an 'n' to quit the program at any time.");

Scanner keyboard = new Scanner(System.in);

try{
System.out.println("Please enter number 1: ");
userGuess1 = keyboard.nextInt();

System.out.println("Please enter number 2: ");
userGuess2 = keyboard.nextInt();

System.out.println("Please enter number 3: ");
userGuess3 = keyboard.nextInt();

do{
if (userGuess1 == secretNum1 && userGuess2 == secretNum2 && userGuess3 == secretNum3){
System.out.println("CONGRATUALTIONS, ALL THREE NUMBERS WERE GUESSED CORRECTLY!");}
if (userGuess2 == secretNum2 && userGuess3 == secretNum3 && userGuess1 != secretNum2){
System.out.println("Number 2 and 3 are correct.");
System.out.println("Please enter number 1: ");
userGuess1 = keyboard.nextInt();}
if (userGuess1 == secretNum1 && userGuess3 == secretNum3 && userGuess2 != secretNum2){
System.out.println("Number 1 and 3 are correct.");
System.out.println("Please enter number 2: ");
userGuess2 = keyboard.nextInt();}
if (userGuess1 == secretNum1 && userGuess2 == secretNum2 && userGuess3 != secretNum3){
System.out.println("Number 1 and 2 are correct.");
System.out.println("Please enter number 3: ");
userGuess3 = keyboard.nextInt();}
}
while(userGuess1 != secretNum1 || userGuess2 != secretNum2 || userGuess3 != secretNum3);
if (userGuess1 == secretNum1 && userGuess2 == secretNum2 && userGuess3 == secretNum3){
System.out.println("CONGRATUALTIONS, ALL THREE NUMBERS WERE GUESSED CORRECTLY!");}
}
catch(InputMismatchException inputMismatchException ){
System.out.println("Goodbye!");
System.exit(0);}
}
}

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
1)
You'll print congratulations 2 times if you got it on the first guess.

2)
&& userGuess1 != secretNum2
Wrong variable.

3)
while(userGuess1 != secretNum1 || userGuess2 != secretNum2 || userGuess3 != secretNum3);
if (userGuess1 == secretNum1 && userGuess2 == secretNum2 && userGuess3 == secretNum3){
That if-statement is pretty stupid becaue there's no way you will reach that statement without it being true (due to while condition)
For now at least, since there is no break; anywhere in the while-loop.
(You may however consider leaving it, as when you are going to check for 'n' you may leave the while-loop without it being true.)

4)
A cleaner way than a try-catch is, since you're using the Scanner class anyway, to use the hasNextInt() method.
Something like:
System.out.println("Please enter number 1: ");
if( keyboard.hasNextInt() ){
    userGuess1 = keyboard.nextInt();
} else {
    //user entered poop.
    keyboard.readLine(); //read whatever was typed, and don't do anything with it... You can check for 'n' here.
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users