-2
import static java.lang.System.out;
import enhanced_for_loop.Symbol;
public class enhanced_for_loop2 {
enum Symbol {
cherry, lemon, kumquat, rutabaga
}
public static void main (String args[]){
for (Symbol leftReel: Symbol.values()){
out.print (leftReel);
out.print(" ");
for (Symbol middleReel: Symbol.values()){
out.print (middleReel);
out.print(" ");
}
for (Symbol rightReel: Symbol.values()){
out.println(rightReel);
}
}
}
}
輸出:
cherry cherry lemon kumquat rutabaga cherry
lemon
kumquat
rutabaga
lemon cherry lemon kumquat rutabaga cherry
lemon
kumquat
rutabaga
kumquat cherry lemon kumquat rutabaga cherry
lemon
kumquat
rutabaga
rutabaga cherry lemon kumquat rutabaga cherry
lemon
kumquat
rutabaga
代碼2:
import static java.lang.System.out;
public class enhanced_for_loop {
enum Symbol {
cherry, lemon, kumquat, rutabaga
}
public static void main (String args[]){
for (Symbol leftReel: Symbol.values()){
for (Symbol middleReel: Symbol.values()){
for (Symbol rightReel: Symbol.values()){
out.print (leftReel);
out.print(" ");
out.print (middleReel);
out.print(" ");
out.println(rightReel);
}
}
}
}
}
輸出:
cherry cherry cherry
cherry cherry lemon
cherry cherry kumquat
cherry cherry rutabaga
cherry lemon cherry
cherry lemon lemon
cherry lemon kumquat
cherry lemon rutabaga
cherry kumquat cherry
cherry kumquat lemon
cherry kumquat kumquat
cherry kumquat rutabaga
cherry rutabaga cherry
cherry rutabaga lemon
cherry rutabaga kumquat
cherry rutabaga rutabaga
lemon cherry cherry
lemon cherry lemon
lemon cherry kumquat
lemon cherry rutabaga
lemon lemon cherry
lemon lemon lemon
lemon lemon kumquat
lemon lemon rutabaga
lemon kumquat cherry
lemon kumquat lemon
lemon kumquat kumquat
lemon kumquat rutabaga
lemon rutabaga cherry
lemon rutabaga lemon
lemon rutabaga kumquat
lemon rutabaga rutabaga
kumquat cherry cherry
kumquat cherry lemon
kumquat cherry kumquat
kumquat cherry rutabaga
kumquat lemon cherry
kumquat lemon lemon
kumquat lemon kumquat
kumquat lemon rutabaga
kumquat kumquat cherry
kumquat kumquat lemon
kumquat kumquat kumquat
kumquat kumquat rutabaga
kumquat rutabaga cherry
kumquat rutabaga lemon
kumquat rutabaga kumquat
kumquat rutabaga rutabaga
rutabaga cherry cherry
rutabaga cherry lemon
rutabaga cherry kumquat
rutabaga cherry rutabaga
rutabaga lemon cherry
rutabaga lemon lemon
rutabaga lemon kumquat
rutabaga lemon rutabaga
rutabaga kumquat cherry
rutabaga kumquat lemon
rutabaga kumquat kumquat
rutabaga kumquat rutabaga
rutabaga rutabaga cherry
rutabaga rutabaga lemon
rutabaga rutabaga kumquat
rutabaga rutabaga rutabaga
你能詳細一點進一步你不太明白?簡單的答案是兩個代碼示例是不同的,這就是爲什麼有不同的輸出。 –
我相信現在它已經正確縮進了,它應該很明顯不是嗎? –
我所看到的區別在於,在第一個示例中,最後一個'for'循環處於第一個(最外部)循環的範圍內,而在第二個示例中,最後一個for循環處於第二個範圍內(中)循環。 – trooper