gpt4 book ai didi

java - 当我将 Final 变量设置为 4 时,如何确保删除顶部 4 行而不是底部 4 行?

转载 作者:行者123 更新时间:2023-12-02 09:25:54 24 4
gpt4 key购买 nike

我正在尝试打印沙漏图形。最初,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;
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("+----------------+");
}

关于java - 当我将 Final 变量设置为 4 时,如何确保删除顶部 4 行而不是底部 4 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com