java循环小节(易错总结 + 新增特性)

3-1-1舍入误差问题

在循环中,检测俩个浮点数是否相等需要格外小心。详情请看2-1-3。

for(double x = 0;x != 10;x += 0.1);

上段代码永远无法结束。

3-1-2java14引入的switch语句

1.新版本的swicth

import java.util.Scanner;
public class Study {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int choice = scan.nextInt();
        switch (choice){
            case 1 -> System.out.println(1);
            case 2 -> System.out.println(2);
            case 3 -> System.out.println(3);
            default -> System.out.println("Bad Input");
        }
    }
}

case的标签可以是

  1. 类型为char、byte、short或int类的常量表达式
  2. 枚举常量
  3. 字符串常量
  4. 多个字符串,用字符串间隔
public class Study {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int seasonNode = 0;              //数值
        switch (seasonNode){
            case 0 -> System.out.println("Spring");
            case 1 -> System.out.println("Summer");
            case 2 -> System.out.println("Fall");
            case 3 -> System.out.println("Winter");
            default -> System.out.println("???");
        }
        String school = "沈阳师范大学";   //字符串
        switch(school){
            case "沈阳师范大学" -> System.out.println("0001");
            case "沈阳工业大学" -> System.out.println("0010");
            case "沈阳大学" -> System.out.println("0011");
            case "沈阳航空航天大学" -> System.out.println("0100");
            default -> System.out.println("can't find anwser");
        }
        String a = "笔";
        switch (a){
            case "笔","墨","纸","砚" -> System.out.println("文房四宝");
            default -> System.out.println("其他");
        }
    }
}

输出:
Spring
0001
文房四宝

2.yiled

在switch中,新增了一个yield关键字,他和break很像,都能终止循环,不过yield还会生成一个值,就是swtich表达式的值。

public class Study {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = switch (1){
            case 1 -> {
                System.out.println("Spring");
                yield  6;
            }
            default -> -1;
        };
        System.out.println(a);
    }
}

上段代码的输出为:
Spring
6

3-1-3中断流程的语句

java提供了一种带标签的break,允许跳出多重循环。我们可以在最外层循环上添加一个以:结尾的表示符作为循环的标签,当循环内有语句break + 标签;时,结束循环。

public class Study {
    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int i = 0,j = 0;
        read_data:
        for( i = 0;i < 10;i++){
            for( j = 0;j < 10;j++){
                if(j == 2){
                    break read_data;
                }
            }
        }
        System.out.println(i + " " + j);
    }
}

上段代码输出为:
0 2
当j == 2时,在read_data底的整个for循环结束,i 执行了一次,j执行了2次。

3-1-4for each循环

java有一个功能很强的循环结构,可以用来依次处理数组(或者任何其他元素的集合)中的每个元素,而不必考虑指定索引值。
形式 : for(varible : collection) statement
类型 数组(其他) 循环体

public class Study {
    public static void main(String[] args) {
        int []a = new int[]{1,2,3,4,5};
        for(int elements : a){
            System.out.println(elements);
        }
    }
}

输出结果为:
1
2
3
4
5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值