javaSE学习:第三章、流程控制

1、switch语句:

 Scanner scanner = new Scanner(System.in);
        System.out.println("请输入水果名:");
        String fruit = scanner.next();
        switch (fruit) {
            case "苹果":
            case "小苹果":
                System.out.println("苹果、小苹果19元/斤");
                break;
            case "香蕉":
                System.out.println("香蕉8元/斤");
                break;
            case "葡萄":
                System.out.println("葡萄39元/斤");
                break;
            default:
                System.out.println("无此水果");

        }

1.1、Switch语句细节:

1、表达式数据类型与case后面常量类型一致,或者是可以自动转换的类型。
2、Switch中返回值只能是:byte、short、int、char、enum、String。
3、case里面的值必须是常量,不能是变量。
4、break是用来跳出循环的,不写会全部执行。

2、for循环:

// 三种特殊写法:
        int i = 0;
        for ( ; i<= 10 ; ) {
            System.out.println("hello");
            i++;
        }
        System.out.println(i);  // 11
        
        for(;;) {  // 死循环
            System.out.println("hello");
        }
        
        int count = 4;
        for (int i1 = 0,j = 0; i < count; i++, j+=2) {
            System.out.println("i" + i + "j" + j);
        }

3、while循环、do while循环:

        int s = 1;
        while (s <= 100) {
            if (s % 3 == 0) {
                System.out.println(i);
            }
            i++;
        }
        
        int n = 1;
        do {
            System.out.println(n);
            n++;
        } while (i <= 10);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值