add final way of check division by zero for lab1
This commit is contained in:
parent
6ab6cc17db
commit
e910ee6e4d
|
@ -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);
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue