import java.util.Scanner;
import java.util.Random;
public class dice
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
Random rnd = new Random();
int correctguesses;
correctguesses = 0;
int r = rnd.nextInt(12) + 1;
System.out.print("guess a number ");
int x = in.nextInt();
if (x != r)
System.out.print(" Do you think the next number will be higher HI or
lower LO than " + x );
int y = in.nextInt();
String guess = in.next();
boolean correct = false;
if (guess.equals("HI"))
{
correct = ( x > y);
}
else if (guess.equals("LO"))
{
correct = (x < y);
}
System.out.println("The number was " + x + " and you guessed " + y +
" so you " + (correct ? "ROCK!" : "SUCK!"));
}
}
this is the program in its full, i am trying to capture input from the
text file.. thanks very much for your help
patrick
Roedy Green - 28 Nov 2005 17:44 GMT
On 28 Nov 2005 09:32:39 -0800, "patrick_woflian"
<gingercrock@hotmail.com> wrote, quoted or indirectly quoted someone
who said :
>else if (guess.equals("LO"))
> {
> correct = (x < y);
> }
what if answered something else besides HI or LO

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Michael Redlich - 28 Nov 2005 18:10 GMT
Patrick:
Thanks for posting all the code for this application. I will
definitely evaluate by tonight.
In the meantime, you may be able to get something out of the example I
found on my CD. It was a small application that Chuck Allison
published in one of his articles a few years ago. See
http://www.freshsources.com/ for more about Chuck Allison's work. The
code is as follows:
import java.io.*;
public class HiLo
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
boolean done = false;
while(!done)
{
boolean found = false;
int lo = 1;
int hi = 100;
int guess = 0;
while(!found && lo <= hi)
{
guess = (lo + hi) / 2;
System.out.println("Is it " + guess + "?");
char r = in.readLine().toUpperCase().charAt(0);
if(r == 'H')
lo = guess + 1;
else if(r == 'L')
hi = guess - 1;
else if(r != 'Y')
System.out.println("Try again...");
else
found = true;
}
if(lo > hi)
System.out.println("You cheated!");
else
System.out.println("Your number was " + guess);
System.out.println("Want to play again?");
done = in.readLine().toUpperCase().charAt(0) != 'Y';
}
}
}
Please contact me if you have any questions...
Mike.
Oliver Wong - 28 Nov 2005 20:12 GMT
> import java.util.Scanner;
> import java.util.Random;
[quoted text clipped - 41 lines]
> this is the program in its full, i am trying to capture input from the
> text file.. thanks very much for your help
I don't know what file you'r referring to, but your program compiles
without error. As for running it... well... it might not do exactly what you
intend (but then again, you haven't said what you intend for it to do).
I'll just point out that your program seems to be expecting the user to
enter in 2 integers in a row, but the prompts displayed to the user give no
indication of this.
In other words, the program asks me to "guess a number", so I do (I
guessed '3'), and then it asks me "Do you think the next number will be
higher HI or lower LO than 3", so I type in "HI", but the program throws an
exception 'cause it was actually expecting me to enter in an integer, as
seen on the line "y = in.nextInt();".
- Oliver
patrick_woflian - 28 Nov 2005 22:53 GMT
well.. i dont know how wrong my code is.. but i desparetly need help
hehe.. the psuecode solution is as follows:
Roll a 12 sided die to choose a number in the range 1....12
while the user has not made an incorrect guess
ask the user to guess whether they think the next number will be higher
or lower than the current number
prompt the user to enter either "hi" or "lo"
choose a new random number by rolling a 12 sided die
if the users guess was correct
increment a count of correct guesses
update the stroed current number
end if
end while
the game is over - tell the user how many correct guesses they made..
any ideas how to change my program!?!?
patrick
Oliver Wong - 28 Nov 2005 22:59 GMT
> well.. i dont know how wrong my code is.. but i desparetly need help
> hehe.. the psuecode solution is as follows:
[quoted text clipped - 13 lines]
>
> any ideas how to change my program!?!?
I noticed that in your pseudo code, it sounds like you only ever ask the
user for a number once (not sure though, because the "while the user has not
made an incorrect guess" part is a bit vague to me). If that's the case, you
should really re-evaluate why in your Java code you're asking the user for a
number twice.
- Oliver