觉得java标签,在循环控制语句中可以发挥出相当大的威力
简单的分析一下,循环语句标签的用法
continue lable;继续标签所在的循环
break lable;跳出标签所在的循环
1.单层循环
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestLabel {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
int j = 0;
lable: for (int i = 0;; i++) {
System.out.println("input :" + i + "--" + j);
br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if (s.equals("c")) {
continue lable;
} else if (s.equals("b")) {
break lable;
}
}
}
}
在单层循环中continue lable;break lable;和一般的continue 和 break 没什么区别
2.多层循环
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestLabel {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
int j = 0;
lable: while (true) {
j++;
for (int i = 0;; i++) {
System.out.println("input :" + i + "--" + j);
br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if (s.equals("c")) {
continue lable;
} else if (s.equals("b")) {
break lable;
}
}
}
}
}
在多层循环中continue lable;break lable;就和continue 和 break不一样了,
continue lable;继续标签所在的循环,即跳出了for循环,继续while中的循环
break lable;跳出标签所在的循环,即不仅跳出了for循环,同时也跳出了while循环
简单的分析一下,循环语句标签的用法
continue lable;继续标签所在的循环
break lable;跳出标签所在的循环
1.单层循环
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestLabel {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
int j = 0;
lable: for (int i = 0;; i++) {
System.out.println("input :" + i + "--" + j);
br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if (s.equals("c")) {
continue lable;
} else if (s.equals("b")) {
break lable;
}
}
}
}
在单层循环中continue lable;break lable;和一般的continue 和 break 没什么区别
2.多层循环
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestLabel {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
int j = 0;
lable: while (true) {
j++;
for (int i = 0;; i++) {
System.out.println("input :" + i + "--" + j);
br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
if (s.equals("c")) {
continue lable;
} else if (s.equals("b")) {
break lable;
}
}
}
}
}
在多层循环中continue lable;break lable;就和continue 和 break不一样了,
continue lable;继续标签所在的循环,即跳出了for循环,继续while中的循环
break lable;跳出标签所在的循环,即不仅跳出了for循环,同时也跳出了while循环