在 Java 中制作 X 图片
Making an X picture in Java
我正在尝试练习如何制作简单的形状,但在制作这个特定形状时遇到了困难:
X X
X X
X
X X
X X
我想要求用户输入一个尺寸,然后根据尺寸,它会根据这些尺寸制作一个形状(上面的形状是如果用户输入 5 时将打印的形状)。我能够使用此代码制作一个正方形:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = 0;
System.out.print("Enter the size of your shape: ");
size = scan.nextInt();
这将得到用户想要的尺寸。然后为了绘制矩形形状,我使用了这个:
static void squareShape(int size){
for(int i = 0;i < size;i++){
for(int j = 0;j < size; j++){
System.out.print("X");
}
System.out.println();
}
}
如果有人能帮我画 X,将不胜感激:)
将 spaces 替换为点以使其更明显:
X.....X
.X...X
..X.X
...X
..X.X
.X...X
X.....X
你需要做的基本上是弄清楚,对于每一行,在第一个和第二个 X 之前要打印多少 space(尽管中心线上没有第二个 X
) .现在为了简化事情,让我们假设高度总是奇数。
您可以看到第一个 space-count 遵循模式 {0, 1, 2, 3, 2, 1, 0}
,其中 3
是高度 7
的一半,向下舍入。这适用于一个简单的循环,计数到中点附近,自己计算中点,然后再次倒计时:
for firstSpaces = 0 to height/2 - 1 inclusive
output firstSpaces spaces, an X and a newline
output height/2 spaces, an X and a newline
for firstSpaces = height/2 - 1 to 0 inclusive
output firstSpaces spaces, an X and a newline
第二个space计数是一个类似的{5, 3, 1, 0, 1, 3, 5}
,起点是height - 2
,在第一个循环中每次减2,每次循环增加2第二个循环。这基本上修改伪代码如下:
secondSpaces = height - 2
for firstSpaces = 0 to height/2 - 1 inclusive
output firstSpaces spaces and an X
output secondSpaces spaces, an X and a newline
subtract 2 from secondSpaces
output height/2 spaces, an X and a newline
for firstSpaces = height/2 - 1 to 0 inclusive
add 2 to secondSpaces
output firstSpaces spaces and an X
output secondSpaces spaces, an X and a newline
在 Python 3(终极伪代码语言)中进行概念验证:
def x(sz):
secondSpaces = sz - 2
for firstSpaces in range(sz//2):
print(' ' * firstSpaces,end='X')
print(' ' * secondSpaces,end='X\n')
secondSpaces -= 2
print(' ' * (sz//2),end='X\n')
for firstSpaces in range(sz//2 - 1, -1, -1):
secondSpaces += 2
print(' ' * firstSpaces,end='X')
print(' ' * secondSpaces,end='X\n')
print()
x(3)
x(5)
x(7)
x(15)
证明逻辑是正确的:
X X
X
X X
X X
X X
X
X X
X X
X X
X X
X X
X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X
X X
X X
X X
X X
X X
X X
X X
现在您只需要用您选择的语言编写类似的代码,并且(可能)也可以根据需要满足非奇数高度的要求。
我正在尝试练习如何制作简单的形状,但在制作这个特定形状时遇到了困难:
X X
X X
X
X X
X X
我想要求用户输入一个尺寸,然后根据尺寸,它会根据这些尺寸制作一个形状(上面的形状是如果用户输入 5 时将打印的形状)。我能够使用此代码制作一个正方形:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int size = 0;
System.out.print("Enter the size of your shape: ");
size = scan.nextInt();
这将得到用户想要的尺寸。然后为了绘制矩形形状,我使用了这个:
static void squareShape(int size){
for(int i = 0;i < size;i++){
for(int j = 0;j < size; j++){
System.out.print("X");
}
System.out.println();
}
}
如果有人能帮我画 X,将不胜感激:)
将 spaces 替换为点以使其更明显:
X.....X
.X...X
..X.X
...X
..X.X
.X...X
X.....X
你需要做的基本上是弄清楚,对于每一行,在第一个和第二个 X 之前要打印多少 space(尽管中心线上没有第二个 X
) .现在为了简化事情,让我们假设高度总是奇数。
您可以看到第一个 space-count 遵循模式 {0, 1, 2, 3, 2, 1, 0}
,其中 3
是高度 7
的一半,向下舍入。这适用于一个简单的循环,计数到中点附近,自己计算中点,然后再次倒计时:
for firstSpaces = 0 to height/2 - 1 inclusive
output firstSpaces spaces, an X and a newline
output height/2 spaces, an X and a newline
for firstSpaces = height/2 - 1 to 0 inclusive
output firstSpaces spaces, an X and a newline
第二个space计数是一个类似的{5, 3, 1, 0, 1, 3, 5}
,起点是height - 2
,在第一个循环中每次减2,每次循环增加2第二个循环。这基本上修改伪代码如下:
secondSpaces = height - 2
for firstSpaces = 0 to height/2 - 1 inclusive
output firstSpaces spaces and an X
output secondSpaces spaces, an X and a newline
subtract 2 from secondSpaces
output height/2 spaces, an X and a newline
for firstSpaces = height/2 - 1 to 0 inclusive
add 2 to secondSpaces
output firstSpaces spaces and an X
output secondSpaces spaces, an X and a newline
在 Python 3(终极伪代码语言)中进行概念验证:
def x(sz):
secondSpaces = sz - 2
for firstSpaces in range(sz//2):
print(' ' * firstSpaces,end='X')
print(' ' * secondSpaces,end='X\n')
secondSpaces -= 2
print(' ' * (sz//2),end='X\n')
for firstSpaces in range(sz//2 - 1, -1, -1):
secondSpaces += 2
print(' ' * firstSpaces,end='X')
print(' ' * secondSpaces,end='X\n')
print()
x(3)
x(5)
x(7)
x(15)
证明逻辑是正确的:
X X
X
X X
X X
X X
X
X X
X X
X X
X X
X X
X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X
X X
X X
X X
X X
X X
X X
X X
现在您只需要用您选择的语言编写类似的代码,并且(可能)也可以根据需要满足非奇数高度的要求。