JAVAEE细细看 初级 03 - 流程控制

流程控制语句

1. switch

switch(表达式){  // 可以是byte,short,int,char,jdk5可以是枚举,jdk7可以是String。
case 值:。。。;break;
case 值:。。。;break;
case 值:。。。;break;
defalut: 。。。;
}

2. case 穿透

	public static void chooseSeason(){
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入月份:");
		int month = sc.nextInt();

		switch (month) {
			case 12:
			case 1:
			case 2:
				System.out.println("春季");
				break;
			case 3:
			case 4:
			case 5:
				System.out.println("夏季");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("秋季");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println("冬季");
				break;
			default:
				System.out.println("没有这个季节!!");

		}
	}

3. for 循环

for (初始化语句;循环判断语句;条件控制语句) {
循环体语句;
}

// 输出5-10 和 10-5
public static void testFor() {
		for (int i = 5; i <= 10; i++) {
			System.out.println(i);
		}

		System.out.println("===========");

		for (int i = 10; i >= 5; i--) {
			System.out.println(i);
		}
	}

4. 水仙花数
3位数中 每一位的3次方之和,等于这个3位数

// 取1-1000内水仙花数
	public static void suixianhua(){
		for (int i = 100; i < 1000; i++) {
			int ge = i % 10 ;
			int shi = i/10%10;
			int bai = i/100;
			if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i) {
				System.out.println("1-999之间的水仙花数是:" + i);
			}
		}
	}

5. while循环
初始化语句;
while(条件判断语句){
循环体语句;
条件控制语句;
}

// 取水仙花数
	public static void shuixianhua_while(){

		int i = 100;
		int count = 0;
		while (i < 1000) {
			int ge = i % 10;
			int shi = i / 10 % 10;
			int bai = i /100;

			if (ge*ge*ge + shi*shi*shi + bai*bai*bai == i) {
				System.out.println("1-999之间的水仙花数是:" + i);
				count ++;
			}
			i ++;
		}
		System.out.println("1-999之间的水仙花数个数是:" + count);

	}

循环嵌套的执行顺序

6. 随机数 Random

1.import java.util.Random;
2.Random r = new Random();
3.int k = r.nextInt(10);       - - -  [0, 10)
int x = r.nextInt(max-min) + min

7. 猜数字

public static void guess_number(){
		Random r = new Random();
		int x = r.nextInt(100) + 1;

		while(true) {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入一个数字:");
			int k = sc.nextInt();

			// 判断
			if (k > x) {
				System.out.println("大了");
			}else if (k < x) {
				System.out.println("小了");
			}else{
				System.out.println("恭喜!!!");
				break;
			}
		}
	}

8.需求:打印99 乘法表

    public static void list9_9(){
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j < i + 1; j++) {
                System.out.print(j + "*" + i + "=" + i*j + "\t");
            }
            System.out.println();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值