基本语句(java)

一、选择结构语句

1.if条件语句

public class test {
	public static void main(String []args) {
		int num = 5;
		if (num % 2 == 0) {
			System.out.println("num为偶数");
		}else {
			System.out.println("num为奇数");
		}
	}
}

2.switch条件语句

public class test {
	public static void main(String []args) {
		int week = 3;
		switch(week) {
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
				System.out.println("工作日");
			break;
			case 6:
			case 7:
				System.out.println("休息日");
			break;
		}
	}
}

二、循环结构语句

1.while循环语句

public class test {
	public static void main(String []args) {
		int x = 2;
		while (x <= 3) {
			System.out.println("x = " + x);
			x++;
		}
	}
}

2.do-while循环语句

public class test {
	public static void main(String []args) {
		int x = 1;
		do {
			System.out.println("x = " + x);
			x++;
		} while(x <= 3);
	}
}

3.for循环语句

public class test {
	public static void main(String []args) {
		int i, j;
		for (i = 1; i <= 9; i++) {
			for (j = 1; j <= i; j++) {
				System.out.println("*");
			}
			System.out.println("\n");
		}
	}
}

三、跳转语句

1.break语句

public class test {
	public static void main(String []args) {	
		int x = 1;
		while (x <= 5) {
			System.out.println("x = " +x);
			if (x == 3) {
				break;
			}
			x++;
		}
	}
}

2.continue语句

public class test {
	public static void main(String []args) {
		int num = 0;
		for (int i = 1; i <= 100; i++) {
			if (i % 2 == 0) {
				continue;
			}
			num += i;
		}
		System.out.println("num =" + num);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值