67,735
社区成员
public class SumOfEven{
public static void main(String[] args)
{
int i1 = 1,i2 = 1; //define two variables
int sum1 = 0,sum2 = 0;//initialize two viariables
while(i1 <= 1000){ //while(){}
if(0 == i1%2){ //如果i1除以2余数为0也就是i1是偶数
sum1 = sum1+i1; //这个i1加到和里。
}
i1++; //自增
}
System.out.println("use while,1 to 1000,all the even's sum is :"+sum1); //打印和
do{
if(0 == i2%2){
sum2 = sum2+i2;
}
i2++;
}while(i2 <= 1000);
System.out.println("use do-while,1 to 1000,all the even's sum is:"+sum2);
}
}