Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labexer5a;
- import java.util.Scanner;
- import java.util.InputMismatchException;
- public class LabExer5A {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int counter = 0;
- int ans = 0;
- int num = (int)(Math.random()*50+1);
- System.out.println("Correct answer: "+num); //Correct Checker
- boolean correct = false;
- System.out.println("Guess a number from 1 to 50!");
- while(!correct){
- try{
- //System.out.println("Retry Count: "+counter);//retry counter
- ans = sc.nextInt();
- if(ans > 50){
- Exception f = new Exception();
- counter++;
- throw f;
- }else if (ans == num){
- System.out.println("You got it in "+counter+" attempts(s)");
- correct = true;
- }else if (ans > num){
- System.out.println("Too high. Try again.");
- counter++;
- }else if (ans < num){
- System.out.println("Too low. Try again.");
- counter++;
- }
- }catch (InputMismatchException e){
- sc.next();
- System.out.println("Invalid input.");
- counter++;
- }catch (Exception f){
- System.out.println("Out of range.");
- System.out.println("Guess a number from 1 to 50!");
- counter++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement