class Demo{
public static void test1() {
System.out.println("--test1--");
for (int i = 1 , j = i +5 ; (i < 5 && j < 8) ; i++ , j =i *2) {
System.out.println("i = "+i+", j = "+j);
}
}
}
//output
//i = 1, j = 6
//i = 2, j = 4
//i = 3, j = 6
class Demo{
public static void main(String[] args){
int i = 0;
outer:
for(;;){
inner:
for(;i<10;i++){
System.out.println("i = "+i);
if(i==2){
System.out.println("continue");
continue ;
}
if(i==3){
System.out.println("break");
i++;
break;
}
if(i==7){
System.out.println("continue outer");
i++;
continue outer;
}
if(i==8){
System.out.println("break outer");
break outer;;
}
for (int k = 0;k < 5;k++ ) {
if(k==3){
System.out.println("continue inner");
continue inner;
}
}
}
}
}
}
//output
//i = 0
//continue inner
//i = 1
//continue inner
//i = 2
//continue
//i = 3
//break
//i = 4
//continue inner
//i = 5
//continue inner
//i = 6
//continue inner
//i = 7
//continue outer
//i = 8
//break outer
indicator 只能是byte char short int string 和枚举,不能是boolean long float double;
而且看下面的例子也是可以成功运行的
不加break 注意case 穿透。
class Demo{
public static void main(String[] args){
byte i = 0b10;
switch (i){
case 2:
System.out.println(1111);
break;
case 0b11:
System.out.println(2222);
break;
default:
System.out.println("xxxxx");
}
}
}
class Demo{
public static void main(String[] args){
short i = 126;
switch (i){
case (char)23:
System.out.println(1111);
break;
case 0b1111110:
System.out.println(2222);
break;
default:
System.out.println("xxxxx");
}
}
}
原文:https://www.cnblogs.com/Pikzas/p/12158974.html