Suppose that there is int vvod = scanner.nextInt (); How can I work with the entered number now? Compare with him other numbers and TP. I need a check for an integer, but without try-catch. Otherwise, if I enter not a number, the idea gives an error. If I implement the has.NextInt () check, then how can I use it to compare it with other numbers? I ask for your help. Thank you in advance.
import java.util.Scanner; public class Monthes { public static void main(String args[]) { System.out.println("Введи количество дней от 1 до 31"); Scanner sc = new Scanner(System.in); if (sc.hasNextInt()) { if (sc.nextInt() == 28 || sc.nextInt() == 30 || sc.nextInt() == 31) { switch (sc.nextInt()) { case 28: System.out.println("Февраль"); case 30: System.out.println("апрель\nиюнь\nсентябрь\nноябрь"); case 31: System.out.println("январь\nмарт\nмай\nиюль\nавгуст\nоктябрь\nдекабрь"); } } else { if (sc.nextInt() < 28) { System.out.println("Столько дней содержитя в каждом месяце"); } else { System.out.println("Введите кол-во дней от 1 до 31"); } } }else { System.out.println("Вы ввели не число!"); } } }
if (scanner.hasNextInt() { ... // а тут уже создаете переменную int n = scanner.nextInt(); к примеру }. And you just continue to work with the variablen(because the scanner call is waiting for the number to be entered, and you only want to enter it once) - entithat