问题描述
我已经搜索过以找到解决此问题的简单方法。
I have searched through to find a simple solution to this problem.
我有一个名为
printCross(int size,char display)
它接受一个size并使用它接收的高度和宽度的char变量打印一个X.
It accepts a size and prints an X with the char variable it receives of height and width of size.
调用方法 printShape(int maxSize,char display)
接受形状的最大大小并进入循环,向printCross方法发送2的倍数,直到达到最大值。
The calling method printShape(int maxSize, char display)
accepts the maximum size of the shape and goes in a loop, sending multiples of 2 to the printCross method until it gets to the maximum.
这是我的代码,但它没有给我预期的结果。
Here is my code but it is not giving me the desired outcome.
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();
}
}
是不是因为我将这些数字硬编码到循环中?我做了很多数学计算,但不幸的是,只有这样才能达到我想要的输出。
Is it because I hardcoded the figures into the loop? I did a lot of math but unfortunately it's only this way that I have been slightly close to achieving my desired output.
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
请我花上几周的时间,似乎无处可去。谢谢
Please I have spent weeks on this and seem to be going nowhere. Thanks
推荐答案
你要做的第一件事是找到索引之间的关系。假设您有长度 size
的方形矩阵(示例中 size = 5
):
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
您可以注意到的是从的对角线( 0,0)
到(4,4)
,索引是相同的(在代码中这意味着 row == col
)。
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
).
另外,你可以注意到(0,4)$ c的对角线$ c>到
(4,0)
索引总是达到 4
,这是 size - 1
(在代码中这是 row + col == size - 1
)。
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
).
因此在代码中,您将循环遍历行,然后遍历列(嵌套循环)。在每次迭代时,您必须检查是否满足上述条件。逻辑OR( ||
)运算符用于避免使用两个 if
语句。
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.
代码:
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
这篇关于如何使用嵌套循环打印出X.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!