【问题标题】:How to print out an X using nested loops如何使用嵌套循环打印出 X
【发布时间】:2014-06-01 05:21:35
【问题描述】:

我已经搜索过这个问题的简单解决方案。

我有一个方法叫

printCross(int size,char display)

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

调用方法printShape(int maxSize, char display) 接受形状的最大尺寸并进入一个循环,将 2 的倍数发送到 printCross 方法,直到达到最大值。

这是我的代码,但它没有给我想要的结果。

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

拜托,我已经为此花费了数周时间,但似乎无处可去。谢谢

【问题讨论】:

  • 您是否尝试过手动运行程序(使用笔和纸)看看它是如何出错的?
  • 好建议。另一种是使用调试器。
  • - 如果以下答案之一特别有用,请选择该答案旁边的复选符号,或单击向上箭头,或同时单击两者:)

标签: java methods char nested-loops shapes


【解决方案1】:

您要做的第一件事是找到索引之间的关系。假设您有一个长度为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) 的对角线中,索引总和为4,即size - 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

【讨论】:

    【解决方案2】:

    我不会直接给出答案,而是给你一些提示。

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

    但是,正如您所注意到的,您决定何时为 5 的情况打印“x”。

    当且仅当 row = col 或 row + col = size - 1 时检查是否打印 'x'

    【讨论】:

      【解决方案3】:

      对于您的 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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-09-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-04-04
            • 1970-01-01
            • 1970-01-01
            • 2023-04-06
            相关资源
            最近更新 更多