Author |
Message |
cool dude
|
Posted: Sun May 07, 2006 12:22 pm Post subject: guessing game |
|
|
this is my first sort of game i made thanks to wtd tutorials and help
any suggestions on improving code would be appreciated
code: |
import java.io.*;
public class GuessNum1 {
public static void main(String[]args) {
//Declare all variables here
String input2; //stores the input as a string
int input; //stores the converted input as an integer
int floor = 0; //stores the lowest bound that the number can be
int ceiling = 100; //stores the highest bound that the number can be
String ans = "no"; //stores if the user made up a number yet
int guess; //stores the computers guess
String hint2; //stores the hint higher/lower/correct as string
int hint; //stores the hint higher/lower/correct as integer
try{
BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Make a number up in your head between 1 and 100");
System.out.println("Have you made a number up? yes/no");
ans = c.readLine();
}while (ans.equals("no"));
System.out.println("The computer will try to guess your number");
System.out.println("if the computer guessed lower please enter '1'.\nif the computer guessed higher please enter 2.\nif the computer guessed correct please enter 3");
System.out.println("");
do{
guess = (floor + ceiling) / 2;
System.out.println("The computer guesses " + guess);
hint2 = c.readLine();
hint = Integer.parseInt(hint2);
switch(hint){
case 1:
floor = guess;
break;
case 2:
ceiling = guess;
break;
case 3:
System.out.println("The computer guesses correct! ");
break;
}
}
while(hint != 3);
}
catch (NumberFormatException nfe) {
System.out.println("can't convert: ");
}
catch (IOException f) {
System.out.println("error reading input");
}
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
xHoly-Divinity
|
Posted: Wed May 10, 2006 5:08 pm Post subject: (No subject) |
|
|
You should make it so that the program automatically identifies if the computer is >/</=. Just a few if statements |
|
|
|
|
|
black moonrise
|
Posted: Wed May 10, 2006 5:31 pm Post subject: (No subject) |
|
|
I've only been taking 2 months of Java in grade 11 myself, so I really shouldn't be the one making suggestions.
But after I ran your program, I've noticed a few problems which I think is not a part of the initial purpose of your program:
1.) code: | while (ans.equals("no")); |
So basically you're saying that as long as what the user inputed is not 'no', the program proceeds.
As in, if I enter '24ylskdg' or 'I'm not ready', the program still runs.
2.)This is not exactly a coding problem as much as an irony to what you've stated your program does.
Your lowest value possible, aka is '0', while you wanted the user to choose a number between 1 and 100.
Assuming that 1 and 100 are included, your highest value, aka is '100'. In doing so, you're not allowing the program to reach 100.
3.)This is also not a huge error, but for the betterment of your program, you should exit the program not only when the user enters '3', but also when the number that the computer guesses occurs more than once.
As in... if you keep on entering 1,2,1,2... eventually the number will be the correct one, but your program keeps on going and outputing the same number over and over again.
^That suggestion was probably redundant.
But if it was my teacher marking, he'd totally spazz over it
4.) code: | catch (IOException f) {
System.out.println("error reading input"); |
Um... where exactly is this supposed to pop up?
5.) code: | catch (NumberFormatException nfe) {
System.out.println("can't convert: "); |
You should put that in the loop... After the user enters something that is not an int, the message pops up and your program just exits instead of keeping on guessing.
6.)Okay... ALWAYS dummy-proof your program before you submit it to your teacher =A=;;
If I were to enter '6688' when your program asks me whether the number that the computer guessed was correct, your program interpret it as if it was '3'
I haven't yet scrutinized your coding... but I'm assuming that's because of this:
Oh god... I should be doing my java assignment right now `A`
I suck so much in Java ;n; |
|
|
|
|
|
cool dude
|
Posted: Wed May 10, 2006 7:26 pm Post subject: (No subject) |
|
|
xHoly-Divinity wrote: You should make it so that the program automatically identifies if the computer is >/</=. Just a few if statements
i don't know wat your talking about?
1) code: |
while (ans.equals("no")); |
i've tried to fix that so if the user enters any letter or word besides no it will continue. but i can't fix it. i keep getting things like incompatible types if i do
code: |
while (ans = "no");
|
2)i'm trying to put this in a loop but it won't work
code: |
catch (NumberFormatException nfe) {
System.out.println("can't convert: ");
|
3) when u enter 6688 it doesn't think its 3 it just repeats the guess and u have to enter again if its higher or lower or equal.
4) how would u determine if the previous guess is the same as the new guess? |
|
|
|
|
|
rizzix
|
Posted: Wed May 10, 2006 8:36 pm Post subject: (No subject) |
|
|
cool dude wrote:
1) i've tried to fix that so if the user enters any letter or word besides no it will continue. but i can't fix it. i keep getting things like incompatible types if i do
here... (get rid of the do-while)
code: | System.out.println("Make a number up in your head between 1 and 100");
System.out.println("Have you made a number up? yes/no");
ans = c.readLine();
if (ans.equals("no")) return; |
|
|
|
|
|
|
HellblazerX
|
Posted: Wed May 10, 2006 8:37 pm Post subject: (No subject) |
|
|
black moonrise wrote:
code: |
catch (IOException f) {
System.out.println("error reading input");
|
Um... where exactly is this supposed to pop up?
Java requires a try and catch statement whenever an action involving IO is used. You don't have to use a try and catch statement by putting a "throws IOException" at the end of you method declaration:
code: | public static void main (String [] args) throws IOException |
cool dude wrote: xHoly-Divinity wrote:
You should make it so that the program automatically identifies if the computer is >/</=. Just a few if statements
i don't know wat your talking about? Confused
What he means by this is for you to input the number in, and let the computer try to guess it on its own. But if you give the computer the number to begin with, that defeats the whole purpose of this program.
cool dude wrote: 2)i'm trying to put this in a loop but it won't work
code: | catch (NumberFormatException nfe) {
System.out.println("can't convert: "); |
You need a try statement at the top as well. You can use this:
code: | do
{
try
{
guess = (floor + ceiling) / 2;
System.out.println ("The computer guesses " + guess);
hint2 = c.readLine ();
hint = Integer.parseInt (hint2);
switch (hint)
{
case 1:
floor = guess;
break;
case 2:
ceiling = guess;
break;
case 3:
System.out.println ("The computer guesses correct! ");
break;
}
}
catch (NumberFormatException nfe)
{
System.out.println ("can't convert: ");
hint = 0;
}
}
while (hint != 3); |
The last part, where hint = 0, just makes it so the computer loops again without doing anything.
cool dude wrote: 4) how would u determine if the previous guess is the same as the new guess?
Just use an int variable to hold the value of your previous guess, and if the current guess is the same as your old guess, then exit the program. |
|
|
|
|
|
rizzix
|
Posted: Wed May 10, 2006 8:42 pm Post subject: (No subject) |
|
|
cool dude wrote: i keep getting things like incompatible types if i do
code: |
while (ans = "no");
|
-_-... that's an assignment expression.. maybe you meant: ans == "no".. that is wrong too.. it should be: ans.equals("no"); |
|
|
|
|
|
cool dude
|
Posted: Wed May 10, 2006 10:35 pm Post subject: (No subject) |
|
|
HellblazerX wrote:
cool dude wrote: 4) how would u determine if the previous guess is the same as the new guess?
Just use an int variable to hold the value of your previous guess, and if the current guess is the same as your old guess, then exit the program.
how can i exit the program. is there any command to exit the program? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Wed May 10, 2006 10:41 pm Post subject: (No subject) |
|
|
since this is all in the main method.. just "return"... |
|
|
|
|
|
[Gandalf]
|
Posted: Wed May 10, 2006 11:06 pm Post subject: (No subject) |
|
|
Or if it is not in the main method:
code: | System.exit(0); //terminates the program with exit code 0 |
So that you are aware once you get into creating your own methods and classes, which I suggest you start getting into at this point. I would think you usually learn about methods before things like try/catch... |
|
|
|
|
|
cool dude
|
Posted: Thu May 11, 2006 8:47 am Post subject: (No subject) |
|
|
is a method just like a procedure or function in turing? |
|
|
|
|
|
rizzix
|
Posted: Thu May 11, 2006 10:20 pm Post subject: (No subject) |
|
|
it's a mixture..
a method with a void return type is equivalent to the turing procedure, while a return type of anything else is equivalent to the turing function. |
|
|
|
|
|
cool dude
|
Posted: Thu May 11, 2006 10:43 pm Post subject: (No subject) |
|
|
i fixed up most of the stuff i think. rizzix i'm not too sure why u told me to get rid of the do while loop since the main point of the question was to know how to make it so if the user enters something like "sjdhfsdf" it won't continue the program and the only way it will continue is if they enter yes.
***EDIT*** how would i call a method (procedure)?
code: |
import java.io.*;
public class GuessNum1 {
public static void main(String[]args) {
//Declare all variables here
String input2; //stores the input as a string
int input; //stores the converted input as an integer
int floor = 1; //stores the lowest bound that the number can be
int ceiling = 100; //stores the highest bound that the number can be
String ans; //stores if the user made up a number yet
int guess; //stores the computers guess
String hint2; //stores the hint higher/lower/correct as string
int hint = 0; //stores the hint higher/lower/correct as integer
int lastguess = 0; //stores the previou guess the computer guessed
try{
BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
do{
System.out.println("Make a number up in your head between 1 and 99");
System.out.println("Have you made a number up? yes/no");
ans = c.readLine();
}while (ans.equals("no"));
System.out.println("The computer will try to guess your number");
System.out.println("if the computer guessed lower please enter '1'.\nif the computer guessed higher please enter 2.\nif the computer guessed correct please enter 3");
System.out.println("");
do{
try{
guess = (floor + ceiling) / 2;
if (guess == lastguess && hint < 3) {
System.out.println("Don't lie! " + guess + " is your number");
System.exit(0);
}
System.out.println("The computer guesses " + guess);
hint2 = c.readLine();
hint = Integer.parseInt(hint2);
if (hint > 3) {
System.out.println("Incorrect hint! please try again!!!");
hint2 = c.readLine();
hint = Integer.parseInt(hint2);
}
if (hint < 3){
lastguess = guess;
}
switch(hint){
case 1:
floor = guess;
break;
case 2:
ceiling = guess;
break;
case 3:
System.out.println("The computer guesses correct! ");
break;
}
lastguess = guess;
} catch (NumberFormatException nfe) {
System.out.println("can't convert: ");
hint = 0;
}
}
while(hint != 3);
}catch (IOException f) {
System.out.println("error reading input");
}
}
} |
|
|
|
|
|
|
HellblazerX
|
Posted: Fri May 12, 2006 10:08 am Post subject: (No subject) |
|
|
cool dude wrote: ***EDIT*** how would i call a method (procedure)?
if you're calling a method from within the same class, then just use method (parameters). If you're calling from outside that class, then you use Class.method (parameters). |
|
|
|
|
|
[Gandalf]
|
Posted: Fri May 12, 2006 4:15 pm Post subject: (No subject) |
|
|
HellblazerX wrote: If you're calling from outside that class, then you use Class.method (parameters).
Only if your method is static, which it likely shouldn't be. If your new method isn't static, then you would create a new object of your class and call the method using nameOfObject.method() |
|
|
|
|
|
|