add final way of check division by zero for lab1

This commit is contained in:
Oleksii Aleshchenko 2023-03-16 15:07:48 +02:00
parent 6ab6cc17db
commit e910ee6e4d
1 changed files with 13 additions and 8 deletions

View File

@ -18,26 +18,31 @@ public class Lab1 {
final int M = 2; final int M = 2;
final int C = 1; final int C = 1;
boolean wasDivisionByZero = false; // boolean wasDivisionByZero = false;
// todo char // todo char
myLabel:for (int i = A; i <= N && !wasDivisionByZero; i++) { // todo[clear code] think about avoiding brackets
if (i + C == 0) { // todo optimize 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!"); System.out.println("Division by zero!");
wasDivisionByZero = true; wasDivisionByZero = true;
break; //todo flag vs return; break; //todo flag vs return;
} }*/
for (int j = B; j <= M; j++) { for (int j = B; j <= M; j++) {
if (j == 0) { /*if (j == 0) {
System.out.println("Division by zero!"); System.out.println("Division by zero!");
return; return;
// wasDivisionByZero = true; // wasDivisionByZero = true;
// break myLabel; // break myLabel;
} }*/
s += (double) (i / j) / (i + C); s += (double) (i / j) / (i + C);
} }
} }
if (!wasDivisionByZero) { // if (!wasDivisionByZero) {
System.out.println("s = " + s); System.out.println("s = " + s);
} // }
} }
} }