/**This program is a simple number guessing game 
using the random class */

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

public class numGuessing {

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Random rndm = new Random();

        int num = rndm.nextInt(50);
        int numGuess;

        System.out.println("Please try to guess a number 1 to 50 !");
        numGuess = input.nextInt();

        while (numGuess != rndm)    {
             if (numGuess < rndm)    {
                System.out.println("Too low. Try again !");
                numGuess = input.nextInt(); }
            if (numGuess > rndm)    {
                System.out.println("Too high. Tr again !");
                numGuess = inut.nextInt();  }
            if (numGuess == rndm)   {
                System.out.println("Great job. You guessed the correct                                  number.");  }}

This code is suppose to find the random number the computer chooses, but it's giving me a (Incompatible operand types int and Random) error. how do i fix it? Thnx

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by Darren76

rndm is your random number generator.
num (line 3) is the random number.
On line 7,8 etc you compare the guess with (the random number generator) when you should compare it with the random number.


Thank James...i see where my mistake is now...I appreciate it

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.