Java基础语法之for/while循环——求100以内奇数/偶数和

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以内奇数/偶数和

上一篇:奇偶链表


下一篇:剑指offer_调整数组顺序使奇数位于偶数前面