think of a number and allow the user to guess it

import java.util.*;

public class ThinkNumber
{
	public static void main (String args[])
	{
		//generate number in range 1 - 100
		//prompt user to enter guess
		//loop until correct number is guessed
		//check guess and output hint
		//guess is correct - output count
	}
}

I currently have this..

import java.util.*;

public class NewClass1{
    public static void main (String args[]){
      int randomNumber = new Random().nextInt(100) + 1;
      System.out.println(randomNumber);
      Scanner input=new Scanner(System.in);
      System.out.print("Enter number to guess: ");
      int num=input.nextInt();
      while(num!=randomNumber){
          System.out.print("Wrong! Again Guess: ");
          num=input.nextInt();
      }
      System.out.println("Correct!!!!!!!");
   }
}

dont know if someone could help if so that would be great!

what help do you need?

what help do you need?

When I run the project the number already appears.

Maybe because your code includes

System.out.println(randomNumber);

well printing out the random number first defeats the purpose of the user trying to guess the number so get rid of the print statement and the import has to be like this:

import java.util.Scanner;

public class NewClass1{
    public static void main (String args[]){
      Scanner sc = new Scanner(System.in);
      int randomNumber = new Random().nextInt(100) + 1; // this is worng it should be like this: int randomNumber = (int) (Math.random() * 100 + 1); close though.
      System.out.print("Enter number to guess: ");
      int num=input.nextInt();
      while(num!=randomNumber){
          if (num>randomNumber){
              System.out.println("Guess lower!") // this says to guess lower
        } else if (num<randomNumber)[
              System.out.println("Guess higher!") // this says to guess higher
              System.out.print("Wrong! Again Guess: "); // this doesn't need to be here
              num=input.nextInt();
      }
      if (num==randomNumber){
         System.out.println("Correct!!!!!!!");
      }
   }
}

k so this is edited so the random runber isn't shown that what you want now ive tested this myself its all ok remove the stuff that i sayd doesn't need to be there, but its in an infinate loop saying whether you need to guess higher or lower and it will print guess higher or lower depending on the number you put in, try and fix this and you should have it if you fix it properly, if not i will try and help by giving hints.

good luck.

@strongguy12345,

You need to be careful not to give away the solution code. This forum is not for getting solution code but to help other understands what their problem and may give hints to the solution. Giving partial code for the solution is fine as well. However, what you did was giving the whole code solution (not talk about quality of code). That's not good.

@Taywin

oops, i was affraid that something like this was going to happen and it did, sorry i don't mean to critcize that your doing something wrong and this is one screw up that im going to learn from but i do hope what i did helped. thanks for letting me know what i did Taywin, i hope that i think of what im putting and that not to give to much away next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.