我正在为学校做一份家庭作业,作业规定“输入两个单词,并垂直打印一个单词,水平打印一个单词,以便它们相交”。
一个例子:
vertical: coffee
horizontal: suffering
c
o
suffering
f
e
e
当我进入咖啡和痛苦时,我得到以下输出:
vertical: coffee
horizontal: suffering
c
o
suffering
f
f
e
e
我的代码如下:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.print("vertical: ");
String vertical = kb.next().toLowerCase();
System.out.print("horizontal: ");
String horizontal = kb.next().toLowerCase();
boolean indexed = true;
int indexOf = 0;
StringBuilder spaces = new StringBuilder();
while (indexed) {
for (int i = 1; i <= vertical.length()-1; i++) {
String found = vertical.substring(i - 1, i);
spaces.append(" ");
if (horizontal.contains(found)) {
indexOf = i;
indexed = false;
break;
}
}
}
for (int i = 1; i <= vertical.length(); i++) {
if (i == indexOf) {
System.out.println(horizontal);
}
System.out.println(spaces + vertical.substring(i - 1, i));
}
}
紫衣仙女
相关分类