首页 > 解决方案 > 当我将 final 变量设置为 4 时,如何确保删除前 4 行而不是后 4 行?

问题描述

我正在尝试打印沙漏图形。最初,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

标签: java

解决方案


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

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 = LINE - 1; 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 = LINE; 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 = LINE; 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("+----------------+");
      }

推荐阅读