Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class trycatch {
  4.  
  5. public static void main(String[] args) {
  6. int attempt =1;
  7. int answer;
  8. int range=50;
  9. Random randoms = new Random();
  10. int rand = randoms.nextInt(range);
  11. Scanner scan = new Scanner(System.in);
  12.  
  13. while(true){
  14.  
  15. try {
  16.  
  17. System.out.println("GUESS A NUMBER FROM 1 - 50!");
  18. answer = scan.nextInt();
  19. if(answer > range)
  20. throw new ArithmeticException();
  21.  
  22. if(answer < rand ){
  23. System.out.println("TOO LOW");
  24. attempt++;
  25. }
  26. else if (answer > rand){
  27. System.out.println("TOO HIGH");
  28. attempt++;
  29. }
  30. else if (answer == rand){
  31. System.out.println("CORRECT");
  32. System.out.println("YOU GOT IN "+ attempt +" ATTEMPT's ");
  33. break;
  34. }
  35.  
  36. } catch (InputMismatchException a){
  37. System.out.println("Invalid input");
  38. scan.nextLine();
  39. }catch (ArithmeticException b){
  40. System.out.println("Out of range");
  41. }
  42. }
  43.  
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement