繁体   English   中英

如何使用嵌套循环打印X

[英]How to print out an X using nested loops

我经过搜索以找到解决此问题的简单方法。

我有一个方法叫做

printCross(int size,char display)

它接受一个大小,并打印一个带有char变量的X,该变量接收高度和宽度的大小。

调用方法printShape(int maxSize, char display)接受形状的最大大小并循环执行,向printCross方法发送2的倍数,直到达到最大值。

这是我的代码,但没有给我期望的结果。

public static void drawShape(char display, int maxSize)
  {
    int currentSize = 2; //start at 2 and increase in multiples of 2 till maxSize

    while(currentSize<=maxSize)
    {
      printCross(currentSize,display);
      currentSize = currentSize + 2;//increment by multiples of 2
    }
  }

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)  
                  System.out.print(display);  
                if (row == 1 && col == 5)  
                  System.out.print(display);  
                if (row == 2 && col == 4)  
                 System.out.print(display);  
                if ( row == 4 && col == 2)  
                 System.out.print(display);  
                if (row == 5 && col == 1)  
                 System.out.print(display);  
                else  
                  System.out.print(" ");   

            }
            System.out.println(); 
    }
}

是否因为我将数字硬编码到循环中? 我做了很多数学运算,但是不幸的是,只有这样,我才刚刚接近达到期望的输出。

If the printCross() method received a size of 5 for instance, the output should be like this:
x   x
 x x
  x
 x x
x   x

拜托,我已经花了数周的时间,似乎一无所获。 谢谢

您要做的第一件事是找到索引之间的关系 假设您有一个长度为size的方阵(示例中为size = 5 ):

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

您会注意到,在从(0,0)(4,4)的对角线中,索引是相同的(在代码中这意味着row == col )。

此外,您还可以注意到,在从(0,4)(4,0)的对角线中,索引总和为4size - 1 (在代码中为row + col == size - 1 )。

因此,在代码中,您将遍历行,然后遍历列(嵌套循环)。 在每次迭代中,您必须检查是否满足上述条件。 逻辑OR( || )运算符用于避免使用两个if语句。

码:

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();
    }
}

输出:( (size = 5, display = 'x')

x   x
 x x 
  x  
 x x 
x   x

除了直接给出答案外,我还会给您一些提示。

首先,使用嵌套的for循环是正确的。

但是,正如您所注意到的,对于5情况,您确定何时打印'x'。

仅当row = col或row + col = size-1时检查是否打印了“ x”

对于您的printCross方法,请尝试以下操作:

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();
    }
}

啊,我被殴打了xD

这是一个简短的丑陋解决方案,它不使用任何空格字符串或嵌套循环。

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尝试使用此简单代码来打印十字图案。

 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(); } } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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