求一题简单的java编程题答案

2024-11-20 00:34:46
推荐回答(1个)
回答1:

public class Test {

public static void main(String[] args) {
int sum = 0, j;
boolean isPrime = true;
for (int i = 2; i <= 20; i++) {
for (j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
sum += i;
} else {
isPrime = true;
}
}
try {
sum = sum / 0;
} catch (Exception e) {
System.out.println("和为:"+sum+",且除数不能为0");
}
}
}