Computer Science Canada guessing game |
Author: | cool dude [ 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
|
Author: | xHoly-Divinity [ Wed May 10, 2006 5:08 pm ] |
Post subject: | |
You should make it so that the program automatically identifies if the computer is >/</=. Just a few if statements |
Author: | black moonrise [ Wed May 10, 2006 5:31 pm ] | ||||||||||||
Post 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.)
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
Assuming that 1 and 100 are included, your highest value, aka
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.)
Um... where exactly is this supposed to pop up? 5.)
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; |
Author: | cool dude [ Wed May 10, 2006 7:26 pm ] | ||||||
Post 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)
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
2)i'm trying to put this in a loop but it won't work
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? |
Author: | rizzix [ Wed May 10, 2006 8:36 pm ] | ||
Post 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)
|
Author: | HellblazerX [ Wed May 10, 2006 8:37 pm ] | ||||||||
Post subject: | |||||||||
black moonrise wrote:
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:
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
You need a try statement at the top as well. You can use this:
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. |
Author: | rizzix [ Wed May 10, 2006 8:42 pm ] | ||
Post subject: | |||
cool dude wrote: i keep getting things like incompatible types if i do
-_-... that's an assignment expression.. maybe you meant: ans == "no".. that is wrong too.. it should be: ans.equals("no"); |
Author: | cool dude [ Wed May 10, 2006 10:35 pm ] |
Post 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? |
Author: | rizzix [ Wed May 10, 2006 10:41 pm ] |
Post subject: | |
since this is all in the main method.. just "return"... |
Author: | [Gandalf] [ Wed May 10, 2006 11:06 pm ] | ||
Post subject: | |||
Or if it is not in the main method:
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... |
Author: | cool dude [ Thu May 11, 2006 8:47 am ] |
Post subject: | |
is a method just like a procedure or function in turing? |
Author: | rizzix [ Thu May 11, 2006 10:20 pm ] |
Post 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. |
Author: | cool dude [ Thu May 11, 2006 10:43 pm ] | ||
Post 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)?
|
Author: | HellblazerX [ Fri May 12, 2006 10:08 am ] |
Post 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). |
Author: | [Gandalf] [ Fri May 12, 2006 4:15 pm ] |
Post 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() |