Label标签只能用于循环结构,例如for,while,do while
并且Label标签只能标记一个循环结构体。例如:
StringBuilder stringBuilder=new StringBuilder(); sdkLabel: for (int i = 0; i < 10; i++) { System.out.println(stringBuilder.replace(0, stringBuilder.length(), ""+i)); if(i==5) break sdkLabel; }如果标记的不是循环结构体的话会报提示,提示信息:
,标名该标签没有被用
所以不同的循环体要对应不同的Label标签
StringBuilder stringBuilder=new StringBuilder(); sdkLabel: for (int i = 0; i < 10; i++) { System.out.println(stringBuilder.replace(0, stringBuilder.length(), ""+i)); if(i==5) break sdkLabel; } lasss: while (true) { int i=0; System.out.println(stringBuilder.replace(0, stringBuilder.length(), ""+i)); if(i==5) break lasss; }