【发布时间】:2015-09-19 02:37:00
【问题描述】:
我想显示由星号组成的字母A。首先,程序会询问字母的大小,并根据给定的大小将字母缩放为该大小。
这是我到目前为止所拥有的,但我不确定我做错了什么。如您所见,它只打印一行。任何帮助都会很棒。
代码:
import java.util.Scanner;
public class Problem7 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
int input, size, i;
System.out.print("Enter a size: ");
input = kbd.nextInt();
while (input <= 1) {
System.out.print("Enter a size: ");
input = kbd.nextInt();
}
for (int col = 0; col < input; col++) {
for (int row = 0; row < input; row++) {
if (col == 1 || col == input || row == col)
System.out.print("*");
else
System.out.print(" ");
}
}
}
}
结果:
Enter a size: 5
* ***** * * *
【问题讨论】:
-
你认为你的代码应该如何工作,你为什么这么认为?
-
当您需要在下一行打印时,使用
System.out.println()或在要打印的字符串中添加\n,或在System.out.printf()中使用%n。 -
看起来你需要切换你的 for 循环的顺序,在你完成行之后你需要 system.out.println()。
-
所以在我的第一个 for 循环之后我应该有 system.out .print ("*") ?