我正在尝试根据输入打印形状;形状是“x”。输入必须是正奇数整数和任意刷子字符。我已经为用户输入完成了代码,但我需要帮助实际打印形状的代码。以下是我到目前为止的情况:
public class TestProgram {
public static void main(String[] args) {
int height = 5;//Any positive odd int but 5 does not work correctly. Not sure what is going on.
char brush = '*';
for (int row = 0; row < height/2; row++) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
for (int row = (height/2)-1; row >= 0; row--) {
for (int i = row; i > 0; i--) {
System.out.print(" ");
}
System.out.print(brush);
for (int i = (height/2); i >= 2*row; i--) {
System.out.print(" ");
}
System.out.print(brush);
System.out.print("\n");
}
for (int row = 1; row < (height/2)+1; row++ ) {
System.out.print(" ");
}
}
}
答案 0 :(得分:0)
我会从常规开始重复char
n
次。像
private static String repeat(char ch, int count) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(ch);
}
return sb.toString();
}
然后,我希望使用String
和StringBuilder
例程构建repeat
。像
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
StringBuilder sb = new StringBuilder();
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
sb.append(repeat(space, half)).append(brush);
sb.append(System.lineSeparator());
for (int row = half - 1; row >= 0; row--) {
int cols = height - ((row + 1) / 2);
sb.append(repeat(space, row)).append(brush);
sb.append(repeat(space, cols)).append(brush);
sb.append(System.lineSeparator());
}
System.out.println(sb.toString());
由于您尚未了解缓冲io,因此也可以表示为
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
System.out.print(repeat(space, height / 2));
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
System.out.print(repeat(space, row));
System.out.print(brush);
System.out.print(repeat(space, cols));
System.out.println(brush);
}
如果你真的不知道如何创建方法,
int height = 5;
char brush = '*';
char space = ' ';
int half = height / 2;
for (int row = 0; row < half; row++) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}
for (int t = 0; t < height / 2; t++) {
System.out.print(space);
}
System.out.println(brush);
for (int row = (height / 2) - 1; row >= 0; row--) {
int cols = height - ((row + 1) * 2);
for (int t = 0; t < row; t++) {
System.out.print(space);
}
System.out.print(brush);
for (int t = 0; t < cols; t++) {
System.out.print(space);
}
System.out.println(brush);
}