我制作了一个for循环,生成一个从300到200的数字列表,其数量为11或13倍。我的问题是,我怎样才能得到这些数字的总和?
package Week3.Yedkois;
public class Project1 {
public static void main(String[] args){
for (int i = 300; i > 200; i-= 1){ // countdown from 300 to 200
boolean num = (i % 11 == 0 || i % 13 == 0) && !(i % 11 == 0 && i % 13 == 0); // multiples of 11 and 13 but not booth.
if (num == true)
System.out.printf("%-5s", i );
}
}
}
答案 0 :(得分:-1)
public static void main(String[] args){
int sum = 0; // variable to store sum
for (int i = 300; i > 200; i--){ // countdown from 300 to 200
boolean num = (i % 11 == 0 || i % 13 == 0) && !(i % 11 == 0 && i % 13 == 0); // multiples of 11 and 13 but not booth.
if (num == true) {
System.out.printf("%-5s", i );
sum += i; // Calculate sum for every matched number
}
}
System.out.printf("Sum = %-5s", sum);
}