public class HelloWorld { public static void main(String[] args) { int sum = 0; for (int i = 1; i % 3 != 0&&i<=100; i++) { { sum = sum + i; } } System.out.println("1到100之间不能被3整除的数之和为:" + sum); } }
public static void main(String[] args) { int sum = 0; for (int i = 1; i<=100; i++) { if(i%3!=0) { sum = sum + i; } } System.out.println("1到100之间不能被3整除的数之和为:" + sum); }
for循环中变量i满足中间的表达式时继续循环,不满足就跳出。你的条件中,当i=3时,不满足i%3!=0,所以循环跳出了