
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!!!!!!!"); } }
But when the code is run I do not want the number to appear straight away. The user guesses first (if makes sense) ;-)
Thank you

import java.util.*;
public class NewClass1{
public static void main(String args[]){
int randomNumber = new Random().nextInt(100) + 1;
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!!!!!!!");
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.