# Thread: Think of a number Program

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.

Java 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;

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");
}
}```
i am getting this error

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.

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;

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");         }     }```

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Think of a number Program

Java Code:
`int randomNumber =(int)(Math.random)() 1* 100 + 1;`
The expression on the right hand side of this assignment expression doesn't make a lot of sense.

If you get stuck, describe *how* you intend using the result returned by random() to construct your random number.

4. ## Re: Think of a number Program

int randomNumber =(int)(Math.random)() 1* 100 + 1;
What are those (bolded) brackets for?

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## 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.

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 rest
Last edited by kevinn205; 11-17-2011 at 01:25 AM.

7. Member
Join Date
Sep 2011
Posts
69
Rep Power
0

## Re: Think of a number Program

Originally Posted by kevinn205
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;

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");         }     }```

yes i am a beginner thats why i am in this forum..

thanks for your help anyway.. (i have been programming for a couple of days straight now and i shouldn't be making these silly mistakes i know)..

but can you please tell me what the error means

many thanks Andy.

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.

9. Member
Join Date
Sep 2011
Posts
69
Rep Power
0

## Re: Think of a number Program

Originally Posted by kevinn205
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 rest
i am using net beans to put my code in as i use this at uni..

10. ## Re: Think of a number Program

Originally Posted by andnlou2678
but can you please tell me what the error means
Did you read replie 3 & 4? Your brackets are not in the correct order.

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 mentioned
Last edited by kevinn205; 11-17-2011 at 01:36 AM.

12. Member
Join Date
Sep 2011
Posts
69
Rep Power
0

## Re: Think of a number Program

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());

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");
}
}```
what i am getting now is continuing numbers like a matrix.

13. ## Re: Think of a number Program

Do you have a question?

14. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Think of a number Program

Java Code:
`int randomNum =(int)(1 + 100 + 1 *Math.random());`
I'm still not sure you have this right.

The way to check is to print the random number after you have selected it:

Java Code:
```int randomNum =(int)(1 + 100 + 1 *Math.random());
System.out.println("randomNum is " + randomNum);```
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.

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.

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 02:33 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..

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;
}
}
}
}```
just a quick question how can i put in a count so it counts how many turns it taken to get the right number.

many thanks Andy.

18. ## Re: Think of a number Program

Declare a variable before the loop. Increment inside the loop.

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++;

20. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: Think of a number Program

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)
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.

Or has someone already said that?

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•