题目
遍历100以内的偶数,偶数的和,偶数的个数
代码
package l2_for;
/*遍历100以内的偶数,偶数的和,偶数的个数*/
public class ForDemo1 {public static void main(String[] args) {//方法1:int sum1 = 0,count1=0;for (int i = 0; i <= 100; i += 2) {if (i != 0) {sum1 += i;count1++;System.out.println(i);}}System.out.println(count1);System.out.println(sum1);//方法2:int sum2 = 0,count2=0;for (int j = 1; j <= 100; j++) {if (j % 2 == 0) {sum2 += j;count2++;System.out.println(j);}}System.out.println(count2);System.out.println(sum2);}
}