Advertisement
anticlown

Простая проверка для Java

Sep 16th, 2024 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         final int MIN_VALUE = 0;
  6.         final int MAX_VALUE = 100;
  7.         boolean isIncorrect;
  8.         Scanner scanner = new Scanner(System.in);
  9.         int number = 0;
  10.  
  11.         do {
  12.             isIncorrect = false;
  13.  
  14.             try {
  15.                 number = Integer.parseInt(scan.nextLine());
  16.             } catch (NumberFormatException e) {
  17.                 isIncorrect = true;
  18.                 System.out.println("Type error!");
  19.             }
  20.  
  21.             if (!isIncorrect && (number < MIN_VALUE || number > MAX_VALUE)) {
  22.                 isIncorrect = true;
  23.                 System.out.println("Range error!");
  24.             }
  25.         } while (isIncorrect);
  26.         scanner.close();
  27.  
  28.         System.out.print(number);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement