public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 100; i++) {
if(i%2==0){
sum += i;
System.out.print(i+" ");
System.out.print("偶数和为:"+sum);
}
}
}
如果不要0 i初始值给1就可以了 希望能帮到你!
public class Test01 {
public static void main(String[] args) {
int sum = 0;
for(int i = 2 ; i <= 100 ;i++){
if(i%2 == 0){
sum += i;
}
}
System.out.println("2到100之间的偶数和:"+sum);
}
}
int count = 0;
for(int i=2;i<=100;i+=2){
count+=i;
}
System.out.println(count);