2016-10-27 43 views
-2

代碼1:下面兩個代碼的區別以及它爲什麼顯示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 
+0

你能詳細一點進一步你不太明白?簡單的答案是兩個代碼示例是不同的,這就是爲什麼有不同的輸出。 –

+0

我相信現在它已經正確縮進了,它應該很明顯不是嗎? –

+0

我所看到的區別在於,在第一個示例中,最後一個'for'循環處於第一個(最外部)循環的範圍內,而在第二個示例中,最後一個for循環處於第二個範圍內(中)循環。 – trooper

回答

0

我要假定環是什麼正在給你煩惱: 代碼1:

for (Symbol leftReel: Symbol.values()){   //This will go through all the values within the enum of "Symbol" 
     out.print (leftReel);      //Prints each value of the enum "Symbol" 
     out.print(" ");        //Prints a space 

     for (Symbol middleReel: Symbol.values()){ //This will also go through all the values within the enum of "Symbol" 
      out.print (middleReel);     //Prints each value of the enum "Symbol" 
      out.print(" "); 
     } 

     for (Symbol rightReel: Symbol.values()){ //This will also go through all the values within the enum of "Symbol" 
      out.println(rightReel);     //Prints each value of the enum "Symbol" 

     } 
    } 

代碼2:

for (Symbol leftReel: Symbol.values()){      //This will go through all the values within the enum of "Symbol" 

       for (Symbol middleReel: Symbol.values()){  //This will also go through all the values within the enum of "Symbol" 

        for (Symbol rightReel: Symbol.values()){ //This will also go through all the values within the enum of "Symbol" 
         out.print (leftReel);     //This prints the value from the current value within the iteration of "Symbol leftReel: Symbol.values()" 
         out.print(" "); 
         out.print (middleReel);     //This prints the value from the current value within the iteration of "Symbol middleReel: Symbol.values()" 
         out.print(" "); 
         out.println(rightReel);     //This prints the value from the current value within the iteration of "Symbol rightReel: Symbol.values()" 

        } 
       } 
      } 
相關問題