| Author |
help needed on inputting - tearing hair out .. please!!!
|
|
| patrick_woflian 2005-11-28, 9:58 pm |
| hey guys, i need help with a very annoying problem...
i am doing a program where the user inputs whether they think a random
number will be higher or lower than their own guess if it is not
correct... the problem im having is with the following code:
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.nextString();
boolean correct = false;
if (guess.equals("HI"))
{
correct = (guess > y);
}
else if (guess.equals("LO"))
{
correct = (guess < y);
}
System.out.println("The number was " + y + " and you guessed " +
guess + " so you " + (correct ? "ROCK!" : "SUCK!"));
the main problem on the compiler is to do with string and guess....any
ideas what to do? my hair can't take anymore pulling..
| |
| Michael Redlich 2005-11-28, 9:58 pm |
| Hi Patrick:
First, I see that you are trying to compare a string with an int
followed by an assignment to a boolean in the lines:
correct = (guess > y);
correct = (guess < y);
I assume that's where your program is not compiling since there are
three different data types being used here.
What is the type of the variable, in? Are you capturing input from the
command line or a text file?
I may have an example program for what you are trying to accomplish.
It's on my laptop at home, but I have a CD archive here at work, and I
will look for it this afternoon.
Sincerely,
Mike.
| |
| patrick_woflian 2005-11-28, 9:58 pm |
| 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 2005-11-28, 9:58 pm |
| 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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
| |
| Michael Redlich 2005-11-28, 9:58 pm |
| 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 2005-11-28, 9:58 pm |
|
"patrick_woflian" <gingercrock@hotmail.com> wrote in message
news:1133199159.304302.322790@z14g2000cwz.googlegroups.com...
> 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
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 2005-11-28, 9:58 pm |
| 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 2005-11-28, 9:58 pm |
|
"patrick_woflian" <gingercrock@hotmail.com> wrote in message
news:1133218389.092549.289920@g47g2000cwa.googlegroups.com...
> 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!?!?
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
|
|
|
|