第一种:while
public static void main(String[] args) {
int i=1;
int sum=0;
while (i<=100){
i++;
if (i%2==0){
sum=sum+i;
}
}
System.out.println("100以内的偶数和"+sum);
}
}
第二种:do——while
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
i++;
if (i%2==0) {
sum=sum+i;
}
} while (i<=100);
System.out.println(sum);
}
}
第三种:for
public static void main(String[] args) {
int sum=0;
for (int i = 0; i <=100; i++) {
if (i%2==0) {
sum=sum+i;
}
}
System.out.println(+sum);
}
}