From 25173d66d18d6b534d52387f38a008359b15d4bb Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 16 Mar 2023 14:47:04 +0200 Subject: [PATCH] add flag "wasDivisionByZero" for lab1 --- src/Lab1.java | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Lab1.java b/src/Lab1.java index bfe89b0..3ddd967 100644 --- a/src/Lab1.java +++ b/src/Lab1.java @@ -1,7 +1,7 @@ public class Lab1 { public static void main(String[] args) { - System.out.println( (double) 0 / 0 ); - System.out.println( Math.sqrt(-1) ); +// System.out.println( (double) 0 / 0 ); +// System.out.println( Math.sqrt(-1) ); double s = 0; /*for (int i = 1; i <= 3; i++) { // 1) ��������� � i = 1, 2) ���� � <= 3, 3) �������� 䳿 � �� �����, System.out.println(i); // 4) �������������� � �� 1, 5) ����������� �� ����� 2 @@ -12,23 +12,31 @@ public class Lab1 { for (int i = 0; i < array.length; i++) { }*/ - final int A = -1; + final int A = -3; final int B = 1; final int N = 2; final int M = 2; final int C = 1; - - for (int i = A; i <= N; i++) { + boolean wasDivisionByZero = false; +// todo char + for (int i = A; i <= N && !wasDivisionByZero; i++) { if (i + C == 0) { // todo optimize System.out.println("Division by zero!"); + wasDivisionByZero = true; break; //todo flag vs return; } for (int j = B; j <= M; j++) { - s += (double) (i + j) / (i + C); + if (j == 0) { + System.out.println("Division by zero!"); + wasDivisionByZero = true; + break; + } + s += (double) (i / j) / (i + C); } } - - System.out.println("s = " + s); + if (!wasDivisionByZero) { + System.out.println("s = " + s); + } } }