Posted by: Sourav | October 27, 2016

Random Number Guessing game in Java,Java Teacher Sourav,Kolkata 09748184075


import java.util.*;
class a{
public static void main(String args[])
{
Random rn=new Random();
int count=0;

int answer;
int minimum=1;
int maximum=10;
int number=rn.nextInt((maximum – minimum) + 1) + minimum;
System.out.println(number);

do {
System.out.println(“Please guess a number between 1 to 100”);
Scanner sc=new Scanner(System.in);
answer=Integer.parseInt(sc.nextLine());
if (answer<number){
System.out.println(“Too low”);
count++;
}
else if (answer>number){
System.out.println(“Too High”);
count++;
}
else{
count++;
break;
}

}while(number!=answer);
System.out.println(“You have guessed the number in “+ count+ ” chances”);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: