Java 基于用户输入打印形状

Java 基于用户输入打印形状,java,Java,我正在尝试根据输入打印形状;形状是一个“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 w

我正在尝试根据输入打印形状;形状是一个“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(" "); 
        }

    }
}
公共类测试程序{
公共静态void main(字符串[]args){
int height=5;//任何正奇数int但5不能正常工作。不确定发生了什么。
字符刷='*';
对于(int row=0;row0;i--){
系统输出打印(“”);
}
系统输出打印(刷子);
对于(int i=(高度/2);i>=2*行;i--){
系统输出打印(“”);
}
系统输出打印(刷子);
系统输出打印(“\n”);
}
对于(int行=1;行<(高度/2)+1;行++){
系统输出打印(“”);
}
系统输出打印(刷子);
系统输出打印(“\n”);
对于(int行=(高度/2)-1;行>=0;行--){
对于(int i=行;i>0;i--){
系统输出打印(“”);
}
系统输出打印(刷子);
对于(int i=(高度/2);i>=2*行;i--){
系统输出打印(“”);
}
系统输出打印(刷子);
系统输出打印(“\n”);
}
对于(int行=1;行<(高度/2)+1;行++){
系统输出打印(“”);
}
}
}

我将从一个例程开始,重复您的
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();
}
因为您还没有了解缓冲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高度=5;
字符刷='*';
字符空间=“”;
int half=高度/2;
对于(int行=0;行<一半;行++){
int cols=高度-((行+1)*2);
系统输出打印(重复(空格、行));
系统输出打印(刷子);
系统输出打印(重复(空格,cols));
系统输出打印LN(刷子);
}
系统输出打印(重复(空间、高度/2));
系统输出打印LN(刷子);
对于(int行=(高度/2)-1;行>=0;行--){
int cols=高度-((行+1)*2);
系统输出打印(重复(空格、行));
系统输出打印(刷子);
系统输出打印(重复(空格,cols));
系统输出打印LN(刷子);
}
如果你真的不知道如何创建一个方法

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);
}
int高度=5;
字符刷='*';
字符空间=“”;
int half=高度/2;
对于(int行=0;行<一半;行++){
int cols=高度-((行+1)*2);
for(int t=0;t=0;行--){
int cols=高度-((行+1)*2);
for(int t=0;t
代码在中间截断。来吧,如果你想要一个有意义的答案,你需要做得更好。复制并粘贴整个程序。希望这能对你们有所帮助。谢谢你们的建议,但这远远超出了我目前的理解——既然代码对我来说更简单,有可能修复我的代码吗?谢谢。@ClassicCars我修复了(并大大简化了)您的代码。你不明白我的答案的哪一部分?我一点都不明白——这不是你的错,我是一名初学的学生,在我的学习中没有取得很大的进步。如果你明白我的意思的话,这看起来像是胡言乱语。我也没有看到你对原始代码的任何修改。谢谢。@ClassicCars我编辑了我的答案。如果你从头到尾阅读这些例子,你会看到你的代码是如何演变成我的答案的。这更有意义。因此,从长远来看,它们执行完全相同的操作,只是使用不同的方法。我们刚刚结束了关于方法的讲座,所以我知道如何编写方法,我们只是从来没有讨论过缓冲io。这是我正在学习的Java入门课程。谢谢你的帮助。
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);
}