2009年7月29日 星期三

[JAVA] LABEL

我相信還蠻少人會知道JAVA有LABEL或是去使用它

只是剛好最近做題目做到

Given:
22. public void go(){
23. String o = "";
24. z:
25. for(int x=0; x<3; x++){
26. for(int y=0; y<2; y++){
27. if(x == 1) break;
28. if(x==2 && y==1) break z;
29. o = o + x + y;
30. }
31. }
32. System.out.println(o);
33. }
What is the result when the go() method is invoked?

答案是:000120

那下面這一題呢??

Given:
1. public class Breaker{
2. static String o = "";
3. public static void main(String[] args){
4. z:
5. o = o + 2;
6. for(int x=3; x<8; x++){
7. if(x == 4) break;
8. if(x == 6) break z;
9. o = o + x;
10. }
11. System.out.println(o);
12. }
13. }
What is the result?

答案是235嗎??

不是,是Compilation fails

第5行擺錯位置了!

因為一個標籤陳述式必須擺在被加標的陳述式之前,

所以如果第4行跟第5行交換位置的話就OK了!!

好吧,其實應該很多人都知道(跟第一行前後呼應)

小弟資質駑鈍...XDD

科科

沒有留言: