Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class trycatch {
- public static void main(String[] args) {
- int attempt =1;
- int answer;
- int range=50;
- Random randoms = new Random();
- int rand = randoms.nextInt(range);
- Scanner scan = new Scanner(System.in);
- while(true){
- try {
- System.out.println("GUESS A NUMBER FROM 1 - 50!");
- answer = scan.nextInt();
- if(answer > range)
- throw new ArithmeticException();
- if(answer < rand ){
- System.out.println("TOO LOW");
- attempt++;
- }
- else if (answer > rand){
- System.out.println("TOO HIGH");
- attempt++;
- }
- else if (answer == rand){
- System.out.println("CORRECT");
- System.out.println("YOU GOT IN "+ attempt +" ATTEMPT's ");
- break;
- }
- } catch (InputMismatchException a){
- System.out.println("Invalid input");
- scan.nextLine();
- }catch (ArithmeticException b){
- System.out.println("Out of range");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement