Results 1 to 20 of 22
Thread: Think of a number Program
- 11-16-2011, 11:51 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Think of a number Program
Hi i have to make this program.
generate random number and get user to guess what it is
randomly generate a number in range 1 – 100
prompt user to enter a guess
keep prompting for a number until the user guesses correctly
output a hint based on the number guessed:
higher
lower
at the end, output the number of guesses taken to guess correctly
this is the code i have so far.
i am getting this errorJava Code:import java.util.*; public class ThinkNumber { public static void main(String [] args) { Scanner myKeyboard = new Scanner(System.in); int randomNumber =(int)(Math.random)() 1* 100 + 1; System.out.print("Please Enter A Random Number"); int Guess = myKeyboard.nextInt(); int attemps = 1; while (Guess != randomNum) if (Guess > randomNum) { System.out.print("Lower"); } else if (guess < randomNum) { System.out.print("Higher"); } int Guess2 = MyKeyboard.nextInt(); System.out.print(""); { Guess = myKeyboard.nextInt(); } System.out.print("Congratulations"); } }
G:\FPT>javac ThinkNumber.java
ThinkNumber.java:8: error: illegal start of expression
int randomNumber =(int)(Math.random)() 1* 100 + 1;
^
1 error
could somebody please explain where i am going wrong
many thanks Andy.
- 11-17-2011, 12:02 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Re: Think of a number Program
There were many errors in your syntax, I can tell your a beginner so I gave you some examples; just be sure to try to notice some of the changes between these two files
Heres some of the main I noticed
you initialized-- means introduced for future use; a varible that you later recalled by a different name etc.. myKeyboard vs MYKEy
also guess vs Guess;
java is case sensitive so you need both the name and the case to be exactly the same
I had a difficult time coppying your code, made me hit return alot so heres what I got, ln sloppy format
Java Code:import java.util.*; import java.util.Random; public class Thinknumber { public static void main(String [] args) { Scanner myKeyboard = new Scanner(System.in); int randomNumber =(int)(Math.random)() 1* 100 + 1; System.out.print("Please Enter A Random Number"); int Guess = myKeyboard.nextInt(); int attemps = 1; while (Guess != randomNumber) if (Guess > randomNumber) { System.out.print("Lower"); } else if (Guess < randomNumber) { System.out.print("Higher"); } int Guess2 = myKeyboard.nextInt(); System.out.print(""); { Guess = myKeyboard.nextInt(); } System.out.print("Congratulations"); } }
- 11-17-2011, 12:05 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Think of a number Program
The expression on the right hand side of this assignment expression doesn't make a lot of sense.Java Code:int randomNumber =(int)(Math.random)() 1* 100 + 1;
If you get stuck, describe *how* you intend using the result returned by random() to construct your random number.
- 11-17-2011, 12:05 AM #4
Re: Think of a number Program
int randomNumber =(int)(Math.random)() 1* 100 + 1;
What are those (bolded) brackets for?
- 11-17-2011, 12:14 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Think of a number Program
I think sloppy formatting is the last sort of example the OP needs. And the posted code does not address the problem that was described.
-----
Getting the case consistent is important. Standard coding practice addresses this problem *and* allows you to tell at a glance the sort of thing a variable etc represents by starting variables with a lower case letter (guess, not Guess) and classes with an upper case letter. Where two words are run together, camelcase is used eg ThinkNumber (as you had), not Thinknumber.
- 11-17-2011, 12:21 AM #6
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Re: Think of a number Program
It doesnt , I apologize some people with their " anti hand outs" bug me this user has no idea what he is doing and if he has actual intentions to learn it would take him all day even if somone handed him the code for the rng because it would not compile; im under the assumption that he is not using a powerfull Ide like eclipse so he wouldnt be aware of the errors and noone else would inform him of them-- sometimes I wonder how people become moderators
Perhaps just knowing that he made errors would allow him to correct them and focus on the problem at hand while creating a compileable version rather than attempting to fix the problem at hand and continueing to get a compile error;
*System's thinking is what seperates the best from the restLast edited by kevinn205; 11-17-2011 at 12:25 AM.
- 11-17-2011, 12:24 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: Think of a number Program
- 11-17-2011, 12:25 AM #8
Re: Think of a number Program
Get off your high horse. We do not spoonfeed people solutions as that hinders their learning not help it. And how exactly did you help by posting "sloppy" code as you put it? Especially since you didn't even address the one problem they asked about.
- 11-17-2011, 12:26 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
- 11-17-2011, 12:27 AM #10
- 11-17-2011, 12:33 AM #11
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Re: Think of a number Program
Aww well reguarding the errors, the things I told you would create compile errors;
and per these peoples complaints I'll try not to spoon feed;
The primary error "ThinkNumber.java:8: error: illegal start of expression" that your facing is from the statement
int randomNumber =(int)(Math.random)() 1* 100 + 1;
this isnt the proper way to introduce a random number; try looking up some examples to generate a random number from google and you'll likely find what your looking for
the scanner and if statements are correct other than the few errors i mentionedLast edited by kevinn205; 11-17-2011 at 12:36 AM.
- 11-17-2011, 12:50 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: Think of a number Program
what i am getting now is continuing numbers like a matrix.Java Code:import java.util.*; public class ThinkNumber { public static void main(String [] args) { Scanner myKeyboard = new Scanner(System.in); int randomNum =(int)(1 + 100 + 1 *Math.random()); System.out.print("Please Enter A Random Number"); int guess = myKeyboard.nextInt(); int attemps = 1; while (guess != randomNum) if (guess > randomNum) { System.out.print("Lower"); } else if (guess < randomNum) { System.out.print("Higher"); } int guess2 = myKeyboard.nextInt(); System.out.print(""); { guess = myKeyboard.nextInt(); } System.out.print("Congratulations"); } }
- 11-17-2011, 12:56 AM #13
Re: Think of a number Program
Do you have a question?
- 11-17-2011, 12:58 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Think of a number Program
I'm still not sure you have this right.Java Code:int randomNum =(int)(1 + 100 + 1 *Math.random());
The way to check is to print the random number after you have selected it:
If the output isn't what you expect or intend I would repeat the advice from reply #3: describe how you are going to make the random number from the value returned by the random() method. Until you are clear about the (arithmetic) process involved, writing code will be rather hit and miss.Java Code:int randomNum =(int)(1 + 100 + 1 *Math.random()); System.out.println("randomNum is " + randomNum);
- 11-17-2011, 01:09 AM #15
Re: Think of a number Program
Math.random will return a min value of 0.0 and max value of 0.9999....
You then multiply that by 1 which is pointless.
Then add 101 which will yield a min value of 101.0 and max value of 101.9999.
Then cast to int to get final min of 101 and max of 101.
- 11-17-2011, 01:24 AM #16
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Re: Think of a number Program
import java.util.*;
public class ThinkNumber {
public static void main(String [] args) {
Scanner myKeyboard = new Scanner(System.in);
//initialize integer
Integer number;
//this line creates a new random number
Random randomNum = new Random();
//below converts the Random into a integer by taking it adding 1 to the random num which is declared a Int,Next.Int
//it uses 1 + 100 because 100 defaults to 0-99 aka 1-100
number = 1+randomNum.nextInt(100);
//below displays the random number
System.out.println( number);}}
This is the easiest coding to a rng if it helps you
as a general rule of thumb, its a great idea to use syso ctrl space to (System.output.println(""); to display your code to test it as someone else has said, it allows you to locate problems quickly like in this instance you would realize that your rng returns 0
Also if you are doing this for a class consider going to google and searching bucky programming java (thenewboston) hes more than willings to give you the asnwers in his videos and a better teacher than likely everyone on the forum combined when it comes to teaching;)Last edited by kevinn205; 11-17-2011 at 01:33 AM.
- 11-17-2011, 02:03 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: Think of a number Program
ok sorted now thank you all for your help..
just a quick question how can i put in a count so it counts how many turns it taken to get the right number.Java Code:import java.util.Random; import java.util.*; public class ThinkNumber { public static void main(String [] args) { Random generator = new Random(); Scan.KyIn = new Scanner(System.in); int number = generator.nextInt(100) + 1; int on_off = 1; while(on_off == 1) { System.out.print("Guess a number between 1 and 100: "); int guess = kyIn.nextInt(); if(guess < number) { System.out.println("Too Low"); } if(guess > number) { System.ou.println("Too High"); } if(guess == number) { System.out.println("Correct! You win!"); on_off = 0; } } } }
many thanks Andy.
- 11-17-2011, 02:11 AM #18
Re: Think of a number Program
Declare a variable before the loop. Increment inside the loop.
- 11-17-2011, 02:15 AM #19
Member
- Join Date
- Nov 2011
- Posts
- 65
- Rep Power
- 0
Re: Think of a number Program
two new errors kyin vs Kyin, and system ou vs system out
Heres my best suggestion
Basicly you want something to keep count ? make a new integer and set it to 0
everytime you guess high it displays wrong, also add a count ++ meaning counter + 1
everytime your too low same and at the end syso(counter)
make sense?
counter = 0;
counter++;
- 11-17-2011, 02:27 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Think of a number Program
In fact the final guess (the one where they guess the correct number) also counts. So the counter variable should be incremented every time around the loop.everytime you guess high it displays wrong, also add a count ++ meaning counter + 1
everytime your too low same and at the end syso(counter)
Or has someone already said that?
Similar Threads
-
Help with my first program. Random number gen.
By bobmiknob in forum New To JavaReplies: 1Last Post: 09-25-2011, 12:43 AM -
Program that truncates a number
By scottmulla in forum New To JavaReplies: 1Last Post: 03-25-2011, 09:45 PM -
smallest number java program
By elmo in forum New To JavaReplies: 9Last Post: 11-18-2010, 09:29 PM -
Phone number Program ..
By Sary in forum New To JavaReplies: 9Last Post: 03-17-2010, 07:15 PM -
A Number Converting Program!
By WastedxYears in forum New To JavaReplies: 2Last Post: 01-09-2010, 12:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks