From fe362197469f9add0f87b6460c81603d887e5efb Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Tue, 2 May 2023 09:58:31 +0300 Subject: [PATCH] Update Lab 1. --- Lab 1/Lab_1.java | 61 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/Lab 1/Lab_1.java b/Lab 1/Lab_1.java index 2dccbb1..c0c7286 100644 --- a/Lab 1/Lab_1.java +++ b/Lab 1/Lab_1.java @@ -7,32 +7,55 @@ public class Lab_1 { Scanner input = new Scanner(System.in); - System.out.print("Enter n: "); - n = input.nextInt(); - - System.out.print("Enter m: "); - m = input.nextInt(); + do { + try { + System.out.print("Enter n: "); + n = input.nextInt(); + break; + } catch (Exception e) { + System.out.println("N must be an integer."); + input.nextLine(); + } + } while (true); do { - System.out.print("Enter a (can't be above n): "); - a = input.nextInt(); - } while (a > n); + try { + System.out.print("Enter m: "); + m = input.nextInt(); + break; + } catch (Exception e) { + System.out.println("M must be an integer."); + input.nextLine(); + } + } while (true); do { - System.out.print("Enter b (can't be above m): "); - b = input.nextInt(); - } while (b > m); + try { + System.out.print("Enter a: "); + a = input.nextInt(); + break; + } catch (Exception e) { + System.out.println("A must be an integer."); + input.nextLine(); + } + } while (true); + + do { + try { + System.out.print("Enter b: "); + b = input.nextInt(); + break; + } catch (Exception e) { + System.out.println("B must be an integer."); + input.nextLine(); + } + } while (true); input.close(); - if ((a <= 0) && (n >= 0)) { - System.out.println("Error, division by zero, shutting down!"); - System.exit(1); - } - - for (int i = a; i < n; i++) { - for (int j = b; j < m; j++) { - s += i * j / i; + for (int i = a; i <= n; i++) { + for (int j = b; j <= m; j++) { + s += j; } }