Java判断奇偶数的三种方法

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




分别使用for while do-while 循环

public class OddEven {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1、for循环
		for(int i=0;i<=100;i++){
			if(i%2==0){
				System.out.print("偶数:"+i+"\n");
			}
			else{
				System.out.print("奇数:"+i);
			}
		}
		//2、while
		int j=0;
		System.out.println("偶数:"+j);
		while(j<100){
			j+=1;
			if(j%2==0){
				System.out.print("偶数:"+j+"\n");
			}
			else{
				System.out.print("奇数:"+j);
			}
		}
		//3、do-while
		int k=0;
		System.out.println("偶数:"+k);
		do{
			k++;
			if(k%2==0){
				System.out.print("偶数:"+k+"\n");
			}
			else{
				System.out.print("奇数:"+k);
			}
		}
		while(k<100);
	}
}

原文链接:https://www.idaobin.com/archives/927.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

69 ÷ = 69