论坛首页 Java企业应用论坛

java标签-循环控制语句的利器

浏览 2135 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-08  
觉得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循环
   发表时间:2011-04-08  
还可以使用goto的话就更好了
0 请登录后投票
   发表时间:2011-04-08  
没用过啊,这是那个版本的特性啊?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics