public class dp {
public static void main(String[] args){
int num = 2;
do {
//实现累加求和
num+=num;
num = num + 2;
} while (num<=50);
System.out.println(" 50以内的偶数之和为:" + num );
}
}
public static void main(String[] args) {
int num = 2;
int sum=0;
do {
//实现累加求和
sum+=num;//sum=sum+num
num = num + 2;
} while (num<=50);
System.out.println(" 50以内的偶数之和为:" + sum );
}
注意num和sum的区别
public class dp {
public static void main(String[] args){
int num = 2;
do {
//实现累加求和
num+=num; //这句应该是 sum+=num; sum才是总和!
num = num + 2;
} while (num<=50);
System.out.println(" 50以内的偶数之和为:" + num );
}
}
public class Test {
public static void main(String[] args){
int sum = 0;
for (int i = 0; i <=50; i++) {
if(i%2==0){//判断是不是偶数
sum += i;
}
}
System.out.println("50以内的偶数之和为:" + sum );
}
}