public class SumFor{public static void main(String[] args){int sum_odd = 0;int sum_even = 0;for(int i=1;i <=100;i++){if(i % 2 == 0){sum_even += i;}else{sum_odd +=i;}}System.out.println("100以内奇数和为:" + sum_odd + "\t" + "100以内偶数和为:" + sum_even);}}
public class EvenNumber{public static void main(String[] args){int i = 1;int sum_odd = 0;int sum_even = 0;while(i <= 100){if(i % 2 == 0){//System.out.println(i);sum_even += i;}else{sum_odd += i;}i++;}System.out.println("100以内奇数和为:" + sum_odd + "\t" + "100以内偶数和为:" + sum_even);}}
如果觉得《Java基础语法之for/while循环——求100以内奇数/偶数和》对你有帮助,请点赞、收藏,并留下你的观点哦!