下面两个代码的差异以及它为什么显示两个不同的输出?

时间:2016-10-27 16:04:44

标签: java for-loop

代码1:

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

1 个答案:

答案 0 :(得分: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()"

                    }
                }
            }