【发布时间】:2018-03-16 21:31:42
【问题描述】:
我正在尝试创建一个完整的钻石,但我几乎已经完成了所有的工作
T T
Te Te
Tes Tes
Test Test
Test Tes
Tes Te
Te T
T
这些 for 循环是创建菱形的原因,但 spaces.substring 是我遇到困难的地方。有谁知道如何通过单独编辑 space.substring 来解决这个问题?
for (int i = 0 ; i < len; i++)
{
System.out.print(SPACES.substring(0,i));
System.out.print (word.substring (0,i) + System.lineSeparator());
//System.out.print(SPACES.substring(i,i+1));
System.out.print (word.substring (0,i+1));
}
for (int g = len ; g >= 0; g--)
{
System.out.print(SPACES.substring(0,g));
System.out.print (word.substring (0,g) + System.lineSeparator());
System.out.print (word.substring (0,g));
}
如您所见,每个 for 循环的前两行创建菱形的右半部分,最后几行创建左半部分。但是左半部分没有空格子字符串,因为我不知道我会在里面放什么。
应该是这样的:
H
O O
U U
S S
E E
S S
U U
O O
H
【问题讨论】:
-
System.out.print(something + System.lineSeparator())-->System.out.println(something). -
您能否提供所需输出的示例?
标签: java text substring joptionpane mirroring