java - 当我将 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
解决方案
您的代码中有一些特定于第 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("+----------------+");
}
推荐阅读
- xamarin - 2 个具有部分共享 UI 的 xamarin 应用程序?
- tensorflow - 从安装程序安装 bazel 0.21.0 在 ubuntu 18.04.1 上失败
- ruby-on-rails - 保持 Rails 控制台连接到 activerecord?
- sql - 嵌入式 SQL 直接与作为 API 调用
- pycharm - ModuleNotFoundError:没有名为“openalpr_api”的模块
- php - 如何将css写入php片段
- shared-libraries - perl6 NativeCall 在 Darwin 上找不到库
- .htaccess - 使用 .htaccess 或 nginx 配置重写图像规则
- c# - C# - 调用另一个脚本失败
- inline-assembly - 如何将标签的地址加载到powerpc中的寄存器中