Java 当我将最后一个变量设置为4时,如何确保删除顶部的4行而不是底部的4行?

Java 当我将最后一个变量设置为4时,如何确保删除顶部的4行而不是底部的4行?,java,Java,我正在尝试打印沙漏图形。最初,LINE常量变量设置为8,工作正常。但是,当我设置LINE=4时;下半部看起来不错,但上半部打印的是顶部的4行,而不是底部的4行(以匹配沙漏的下半部) 将LINE变量设置为4后,我尝试切换第一个和第三个for循环,以打印沙漏上半部分的底部4行。我还对每个for循环中的变量进行了反复试验,使第一个for循环中的一些变量为负值,以尝试打印底部的4行。我还使用了调试器,没有发现任何语法或逻辑错误 public static final int LINE = 4; pub

我正在尝试打印沙漏图形。最初,LINE常量变量设置为8,工作正常。但是,当我设置LINE=4时;下半部看起来不错,但上半部打印的是顶部的4行,而不是底部的4行(以匹配沙漏的下半部)

将LINE变量设置为4后,我尝试切换第一个和第三个for循环,以打印沙漏上半部分的底部4行。我还对每个for循环中的变量进行了反复试验,使第一个for循环中的一些变量为负值,以尝试打印底部的4行。我还使用了调试器,没有发现任何语法或逻辑错误

public static final int LINE = 4;

public static void question8(){
    System.out.println("+----------------+");

    // UPPER HALF (where the "error" occurs)
    for (int i=1; i<=LINE;i++){
        System.out.print("|");
        for (int j=1; j<i;j++){
            System.out.print(" ");
        }
        System.out.print("\\");
        for (int k=7; k>i-1;k--){//this was the line changed!
            System.out.print("..");
        }
        System.out.print("/");
        for (int j=1;j<i; j++){
            System.out.print(" ");
        }
        System.out.println("|");
    }
    //BOTTOM HALF
    for (int i=1; i<=LINE;i++){ // i controls the #of lines
        System.out.print("|");
        for(int j=8; j>i;j--){ // j controls the # of spaces before / in every line.
            System.out.print(" ");
        }
        System.out.print("/");
        for (int k=0; k<i-1;k++){ // k is to control how many dots we have in every line.
            System.out.print("..");
        }
        System.out.print("\\");
        for (int j=8;j>i;j--){ // j is to control how many spaces we have after \ in every line.
            System.out.print(" ");
        }
        System.out.println("|");
    }
    System.out.println("+----------------+");
}
实际结果:

+----------------+
|\............../|
| \............/ |
|  \........../  |
|   \......../   |
|       /\       |
|      /..\      |
|     /....\     |
|    /......\    |
+----------------+

no error messages

您的代码中有一些特定于第8行的硬编码内容,请按如下所示更新代码,所有数值都将是动态的。(但您需要相应地更改
+--------------------+“

public static final int LINE=4;
公共静态无效问题8(){
System.out.println(“+-------------------------------”;
//上半部分(出现“错误”的地方)
对于(int i=1;i i-1;k--){//这是更改的行!
系统输出打印(“…”);
}
系统输出打印(“/”);
对于(int j=1;ji;j--){//j是控制每行中\之后有多少空格。
系统输出打印(“”);
}
System.out.println(“|”);
}
System.out.println(“+-------------------------------”;
}

您就快到了。有三个for循环需要更改的起始值

// Top half
for (int k=7; k>i-1;k--){//this was the line changed!
    System.out.print("..");
}
...
// Bottom half
for(int j=8; j>i;j--){ // j controls the # of spaces before / in every line.
    System.out.print(" ");
}
...
for (int j=8;j>i;j--){ // j is to control how many spaces we have after \ in every line.
    System.out.print(" ");
}
在第一个for循环中,
k
开始时应等于
LINE-1
。在接下来的两个for循环中,
j
应初始化为等于
LINE
。这样,函数完全依赖于值
LINE
,而不是一些硬编码值。但是,您仍必须更改以下打印状态函数顶部和底部的元素

System.out.println("+----------------+");
根据
变量仅打印一定数量的破折号

// Top half
for (int k=7; k>i-1;k--){//this was the line changed!
    System.out.print("..");
}
...
// Bottom half
for(int j=8; j>i;j--){ // j controls the # of spaces before / in every line.
    System.out.print(" ");
}
...
for (int j=8;j>i;j--){ // j is to control how many spaces we have after \ in every line.
    System.out.print(" ");
}
System.out.println("+----------------+");