# Random Number = Input Question

Page 1 of 1

## 3 Replies - 201 Views - Last Post: 13 September 2012 - 05:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291779&amp;s=67c575dfdbfb2c481045303b4bd9222c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ritsamillion

Reputation: 0
• Posts: 4
• Joined: 13-February 11

# Random Number = Input Question

Posted 12 September 2012 - 07:49 PM

Hello,

First off, I am a noob so my experience in writing code is only about a month long.

I wrote a little program that asks the user to pick a number between 1 and 10.

I've also coded a random number generator to pick a number between 1 and 10.

My question is, how do I write the code so that if the user guesses a number and it happens
to equal the random number, a message is displayed.

Here is what I got so far:

```import java.util.Random;
import java.util.Scanner;

public class practice {
public static void main(String args[]){

//Calling random object and calling it
//thingy
Random thingy = new Random();
//Calling a Scanner and calling it guess
Scanner guess = new Scanner(System.in);

int number;
int yourguess;

System.out.println("Enter a number between 1 and 10, if you guess the correct number there will be a surprise message: ");
yourguess = guess.nextInt();

for (int counter=1; counter<=10; counter++){
number = 1+thingy.nextInt(10);
if (number = yourguess)
System.out.println("Holy cow, you guessed the correct number!");
else
System.out.println("You guessed the wrong number");
}

}
}

```

The problem is I get and error stating: Type mismatch: cannot convert from int to boolean

I understand what it's saying but how do I fix it?

Thanks,
Shane

Is This A Good Question/Topic? 0

## Replies To: Random Number = Input Question

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8029
• Posts: 31,164
• Joined: 06-March 08

## Re: Random Number = Input Question

Posted 12 September 2012 - 08:04 PM

the problem is here
```    for (int counter=1; counter<=10; counter++){
number = 1+thingy.nextInt(10);

```

you call for 10 different random number

You should:
- call thingy.nextInt(10) + 1 only once to get that random number
- compare it to the user input

### #3 HopelessDev

Reputation: 4
• Posts: 136
• Joined: 10-August 12

## Re: Random Number = Input Question

Posted 12 September 2012 - 08:04 PM

In this line :
```
if (number = yourguess)

```

What you're doing is that your are assigning number with the value of yourguess. When checking for equality of numbers use == not =.

This post has been edited by HopelessDev: 12 September 2012 - 08:05 PM

### #4 ritsamillion

Reputation: 0
• Posts: 4
• Joined: 13-February 11

## Re: Random Number = Input Question

Posted 13 September 2012 - 05:42 AM

Ok, thanks guys/gals, I'll try it.

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).