简体   繁体   中英

Learning Java confused by "maze" pattern

Ok everyone i'm taking a free course on EDX and I can't get some of the activities earlier In lesson to work so I had to dive head first into this.

The program creates a "zig zag" pattern, the size of which depending on the int numTiles. I am confused by this because running through the program in my head I think it would way work waaay differently. I don't know why it prints 1 for the entire line depending on the numTiles. Wouldn't the program stop at the point? Why don't J and I increase everytime? What case would be the spaces? Do I or J ever go over the int numtiles? How does J ever equal 0 except in the first time it runs? Please help me wrap my head around this.

public class Main {

    public static void main(String[] args) {
        int numTiles = 8;
        for(int i=0; i<numTiles;i++){
            for(int j=0; j<numTiles;j++){
                if(i%2==0){
                    System.out.print("1");  
                }else if ((i-1)%4==0 && j==numTiles-1){
                    System.out.print("1");              
                }else if((i+1)%4==0 && j==0){
                    System.out.print("1");  
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

I think your misunderstanding may boil down to vague variable names and difficult-to-read code style. Here's how I would translate the program into a more understandable form:

public class Main {
    public static void main(String[] args) {
        int rows = 8;

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < rows; col++) {
                if (row % 2 == 0 ||
                    row % 4 == 3 && col == 0 ||
                    row % 4 == 1 && col == rows - 1) {
                    System.out.print("1");  
                }
                else {
                    System.out.print(" ");
                }

                try { Thread.sleep(100); } // for demonstration purposes
                catch (Exception e) {}
            }

            System.out.println();
        }
    }
}

The outer loop controls the printing of the rows. There will be 8 rows in the output in this case. The inner loop controls printing of the columns (also 8). Cells are printed left to right in each row.

Now, the only question left is for each cell, do we print a "1" or a blank space? The conditional says print a "1" if the row is an even number OR if the row is an odd number and the current column index is at one end or another. Otherwise, print a space.

I also added a Thread.sleep call which will delay printing, allowing you to watch the program run cell by cell. I hope this helps clarify matters for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM