3 Replies - 201 Views - Last Post: 13 September 2012 - 05:42 AM Rate Topic: -----

#1 ritsamillion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8029
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 HopelessDev  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 ritsamillion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1