Computer Science Canada

Number guessing game in"3 tries"

Author:  nelsonkkyy [ Wed Oct 13, 2010 5:44 pm ]
Post subject:  Number guessing game in"3 tries"

i have a problem with the number guessing game.
I do not know to make it in three tries &This is all i got so far:
Quote:

import java.io.*;
import java.util.Random;
public class GuessWhatItIs
{
public static void main (String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
Random rand = new Random ();
int secretNum = rand.nextInt (10) + 1;
int guess = 0;
int count = 0;

do
{
++count;
System.out.print ("Guess a number between 1 and 10 in 3 tries: ");
System.out.flush ();
String line = br.readLine ();
guess = Integer.parseInt (line);



for (tries = 1 ; tries <= 3 ; tries++)

if ((guess == secretNum + 1) || (guess == secretNum - 1))
System.out.println (" Hot ! Try Again ! ");
else if ((guess == secretNum + 2) || (guess == secretNum - 2))
System.out.println (" Warm ! Try Again ! ");
else if ((guess == secretNum + 3) || (guess == secretNum - 3))
System.out.println (" Warm ! Try Again ! ");
else if ((guess == secretNum + 4) || (guess == secretNum - 4))
System.out.println (" Warm ! Try Again ! ");
else if ((guess == secretNum + 5) || (guess == secretNum - 5))
System.out.println (" Cold ! Try Again ! ");
else if ((guess == secretNum + 6) || (guess == secretNum - 6))
System.out.println (" Cold ! Try Again ! ");
else if ((guess == secretNum + 7) || (guess == secretNum - 7))
System.out.println (" Cold ! Try Again ! ");
else if ((guess == secretNum + 8) || (guess == secretNum - 8))
System.out.println (" Cold ! Try Again ! ");
else if ((guess == secretNum + 9) || (guess == secretNum - 9))
System.out.println (" Not even close ! Try Again ! ");
else if ((guess == secretNum))
System.out.println (" Your guess was correct ! The answer is " + secretNum);
}

while (guess != secretNum);


System.out.print (" It took you ");
System.out.print (count);
System.out.println (" guesses");

}
}

Can anyone help how tp make it in only 3 tries??

Author:  DanShadow [ Wed Oct 13, 2010 7:32 pm ]
Post subject:  RE:Number guessing game in"3 tries"

nelsonkkyy wrote:
i have a problem with the number guessing game.
I do not know to make it in three tries &This is all i got so far:


Here is 1 solution..

code wrote:

boolean guessedIt = false;
Random rand = new Random ();
int secretNum = rand.nextInt (10) + 1;
int tries = 0;

for (i=1; i<=3; i++)
{
if (guessedIt==false)
{
System.out.print ("Guess a number between 1 and 10 in 3 tries: ");
System.out.flush ();
String line = br.readLine ();
guess = Integer.parseInt (line);

if (guess==secretNum)
{
guessedIt=true;
tries = i;
}
}
}

if (guessedIt==true)
{
System.out.print("You guessed the secret number in ");
System.out.print(tries);
System.out.println(" tries!");
} else {
System.out.println("You did not guess the secret number within 3 tries! Sorry!");
}

Author:  nelsonkkyy [ Wed Oct 13, 2010 10:46 pm ]
Post subject:  Re: Number guessing game in"3 tries"

tks you! but i still got an error from the first line
Quote:

boolean guessedIt = false;

Author:  TheGuardian001 [ Wed Oct 13, 2010 11:43 pm ]
Post subject:  Re: Number guessing game in"3 tries"

That wasn't a complete program.


Your code was actually technically correct, except you surrounded it in a do{}while loop.
Your current code uses this structure:
code:

do
{
    get input
    for (3 times)
    {
        check answer
        if (correct answer)
            output
            exit
    }
}while (incorrect answer)
output number of tries

You need to restructure your code so that you don't use a do{}while. You already have a for loop that gives them three tries, so the do{}while is unnecessary. Your structure should be:
code:

for (3 times)
{
    get input
    if (close)
        WARM
    if (not close)
        COLD
    if (correct)
    {
        Good Job!
        output number of tries
        break
    }
}


: