You are on page 1of 1

package mema;

import java.util.*;
public class GuessingGame {

public static void main(String[] args) {


int guess, counter = 0, x;
Scanner sc = new Scanner (System.in);
Random r = new Random();
final int max = 50;
x = r.nextInt(max)+1;
try {
while(true) {
System.out.println("Guess the number between 1--50!");
guess = sc.nextInt();
counter++;

if( guess<1) {
System.out.println("Lesser than the range");
counter--;
}
else if( guess<1) {
System.out.println("Over the range");
counter--;
}
else if(guess<x) {
System.out.println("Too low");
}
else if(guess<x) {
System.out.println("Too high");
}
else if(guess==x) {
System.out.println("You have guessed the right number
"+"("+counter+") "+"attempts");
break;
}

}
}

catch(Exception e) {
System.out.println("Numbers only");
System.out.println("You have guessed the right number "+"("+counter+")
"+"attempts");
counter--;
}
}
}

You might also like