How to print out an X using nested loops

扶醉桌前 提交于 2019-12-04 22:37:49

The first thing you have to do is to find relationships between indices. Let's say you have the square matrix of length size (size = 5 in the example):

  0 1 2 3 4
0 x       x
1   x   x
2     x
3   x   x
4 x       x

What you can notice is that in the diagonal from (0,0) to (4,4), indices are the same (in the code this means row == col).

Also, you can notice that in the diagonal from (0,4) to (4,0) indices always sum up to 4, which is size - 1 (in the code this is row + col == size - 1).

So in the code, you will loop through rows and then through columns (nested loop). On each iteration you have to check if the conditions mentioned above are met. The logical OR (||) operator is used to avoid using two if statements.

Code:

public static void printCross(int size, char display)
{
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            if (row == col || row + col == size - 1) {
                System.out.print(display);
            } else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

Output: (size = 5, display = 'x')

x   x
 x x 
  x  
 x x 
x   x

Instead of giving a direct answer, I will give you some hints.

First, you are right to use nested for loops.

However as you noticed, you determine when to print 'x' for the case of 5.

Check that 'x' is printed if and only if row = col or row + col = size - 1

for your printCross method, try this:

public static void printCross(int size, char display) {
    if( size <= 0 ) {
        return;
    }

    for( int row = 0; row < size; row++ ) {
        for( int col = 0; col < size; col++ ) {
            if( col == row || col == size - row - 1) {
                System.out.print(display);
            }
            else {
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}

ah, I got beaten to it xD

Here's a short, ugly solution which doesn't use any whitespace strings or nested looping.

public static void printCross(int size, char display) {
    for (int i = 1, j = size; i <= size && j > 0; i++, j--) {
        System.out.printf(
              i < j ? "%" + i + "s" + "%" + (j - i) + "s%n"
            : i > j ? "%" + j + "s" + "%" + (i - j) + "s%n"
            : "%" + i + "s%n", //intersection
            display, display
        );
    }
}

Lte's try this simple code to print cross pattern.

class CrossPattern {
        	public static void main(String[] args) {
        		Scanner s = new Scanner(System.in);
        		System.out.println("enter the number of rows=column");
        		int n = s.nextInt();
        		int i, j;
        		s.close();
        		for (i = 1; i <= n; i++) {
        			for (j = 1; j <= n; j++) {
        				if (j == i) {
        					System.out.print("*");
        				} else if (j == n - (i - 1)) {
        					System.out.print("*");
        				} else {
        					System.out.print(" ");
        				}
        			}
        			System.out.println();
        		}
        	}
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!