From e910ee6e4de63b0e70a53075ab9ae2dcb1a9cab4 Mon Sep 17 00:00:00 2001 From: Oleksii Aleshchenko Date: Thu, 16 Mar 2023 15:07:48 +0200 Subject: [PATCH] add final way of check division by zero for lab1 --- src/Lab1.java | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Lab1.java b/src/Lab1.java index e74c2ce..241b984 100644 --- a/src/Lab1.java +++ b/src/Lab1.java @@ -18,26 +18,31 @@ public class Lab1 { final int M = 2; final int C = 1; - boolean wasDivisionByZero = false; +// boolean wasDivisionByZero = false; // todo char -myLabel:for (int i = A; i <= N && !wasDivisionByZero; i++) { - if (i + C == 0) { // todo optimize +// todo[clear code] think about avoiding brackets + if ((A <= -C && -C <= N) || (B <= 0 && 0 <= M)) { + System.out.println("Division by zero!"); + return; + } +/*myLabel:*/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++) { - if (j == 0) { + /*if (j == 0) { System.out.println("Division by zero!"); return; // wasDivisionByZero = true; // break myLabel; - } + }*/ s += (double) (i / j) / (i + C); } } - if (!wasDivisionByZero) { +// if (!wasDivisionByZero) { System.out.println("s = " + s); - } +// } } }