David Ferguson
Hiya!
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners

I dont suppose anybody could help me write a Java program for 'Guess A number'

//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 } }

Thank you

Ads

View Answers

November 1, 2011 at 11:18 AM


import java.util.*;

public class ThinkNumber{
    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!!!!!!!");
   }
}









Related Tutorials/Questions & Answers:

Ads

Advertisement