You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌套for循环实现沙漏图形的Java代码逻辑错误排查求助

问题描述

我正在完成一项作业,需要用嵌套for循环打印沙漏图形:

  • 底部引号数量由用户输入
  • 奇数和偶数输入的区别在于中间符号(偶数为||,奇数为|:|)

目前仅完成底部打印,编写上锥(upCone)部分时,即便将baseVal减2,冒号打印数量仍超出预期,无法定位逻辑错误。

现有代码

import java.util.Scanner;

public class HourglassProject {
    /*
     * Print Base
     * Print Middle
     * Print invPyrimid
     * Print Pyrimid
     */

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        System.out.println("enter the length of the base(# of colons), reccomended 10/12");
        int numLength = userInput.nextInt();
        printBase(numLength);
        upCone(numLength);

        userInput.close();
    }
    
    public static void printBase(int baseNum) {
        String quotation = "";
        for (int i = 0; i < baseNum; i++) {
            quotation += "\"";
        }
        System.out.println("|" + quotation + "|");
        
    }
    public static void upCone(int baseVal) {
        int lineNum = 0; 
        String colon= "";
        String space = "";
        //for loops: Line, Spaces; Colon shrink
        while(baseVal > 0) {
            
            for(int i = 0; i <= lineNum; i++) {
                System.out.print(" ");
                
                for(int j = 1; j <= (baseVal - 2); j++) {
                    colon += ":";
                
                }
                baseVal -= 2;
            }
            lineNum++;
            System.out.println("\\" + colon + "/");
            colon = "";
        }
        
        for(int i = 0; i <= lineNum; i++) {
            space += " ";
        }
        
        if(baseVal % 2 == 0) {
            System.out.println(space + "||");
            
        } else {
            System.out.println(space + "|:|");
        }
        
        
    }
}

期望输出(输入10时)

|""""""""""|
 \::::::::/
  \::::::/
   \::::/
    \::/
     ||
    /::\
   /::::\
  /::::::\
 /::::::::\
|""""""""""|

实际输出(输入10时)

enter the length of the base(# of colons), reccomended 10/12
10
|""""""""""|
 \::::::::/
  \::::::::::/
   \::/
    ||

问题排查与修复

核心错误点

  1. 嵌套循环逻辑混乱:在打印空格的for循环中,同时执行了冒号生成和baseVal递减操作,导致每打印一个空格就重复生成冒号并多次减少baseVal,这是冒号数量异常的根本原因。
  2. 循环终止条件错误:while(baseVal > 0)会导致循环多执行一次(比如输入10时,应该在baseVal=2时停止,而不是到0)。
  3. 空格数量计算错误:打印空格的循环条件i <= lineNum会导致空格数多1个,应该改为i < lineNum。
  4. 奇偶判断失效:循环结束后baseVal已变为负数,此时用baseVal%2判断奇偶完全错误,需使用原始传入的数值判断。

修复后的完整代码

import java.util.Scanner;

public class HourglassProject {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);
        System.out.println("enter the length of the base(# of colons), reccomended 10/12");
        int numLength = userInput.nextInt();
        printBase(numLength);
        upCone(numLength);
        downCone(numLength);
        printBase(numLength);

        userInput.close();
    }
    
    public static void printBase(int baseNum) {
        String quotation = "";
        for (int i = 0; i < baseNum; i++) {
            quotation += "\"";
        }
        System.out.println("|" + quotation + "|");
    }

    public static void upCone(int baseVal) {
        int lineNum = 1;
        int originalBase = baseVal;
        // 循环终止:偶数到2停止,奇数到1停止
        while (baseVal > 2) {
            // 打印前置空格
            for (int i = 0; i < lineNum; i++) {
                System.out.print(" ");
            }
            // 生成当前行的冒号
            String colon = "";
            for (int j = 0; j < baseVal - 2; j++) {
                colon += ":";
            }
            // 打印上锥行
            System.out.println("\\" + colon + "/");
            // 更新参数
            baseVal -= 2;
            lineNum++;
        }

        // 打印中间符号
        String space = "";
        for (int i = 0; i < lineNum; i++) {
            space += " ";
        }
        if (originalBase % 2 == 0) {
            System.out.println(space + "||");
        } else {
            System.out.println(space + "|:|");
        }
    }

    // 补充下锥打印逻辑
    public static void downCone(int baseVal) {
        int lineNum;
        // 根据奇偶确定起始空格数
        if (baseVal % 2 == 0) {
            lineNum = (baseVal / 2) - 1;
        } else {
            lineNum = ((baseVal - 1) / 2) - 1;
        }
        int currentColon = 2;
        while (currentColon <= baseVal) {
            // 打印前置空格
            for (int i = 0; i < lineNum; i++) {
                System.out.print(" ");
            }
            // 生成当前行的冒号
            String colon = "";
            for (int j = 0; j < currentColon; j++) {
                colon += ":";
            }
            // 打印下锥行
            System.out.println("/" + colon + "\\");
            // 更新参数
            currentColon += 2;
            lineNum--;
        }
    }
}

修复说明

  1. 拆分嵌套循环:将空格打印、冒号生成、参数更新的逻辑完全拆分,避免在空格循环中执行其他无关操作。
  2. 修正循环终止条件:while(baseVal > 2)确保上锥最后一行对应冒号数量为0,刚好衔接中间符号。
  3. 修正空格计算:使用i < lineNum控制空格数,保证每行空格数递增1,符合沙漏的缩进规律。
  4. 修复奇偶判断:用原始传入的originalBase进行奇偶判断,避免循环后baseVal变为负数导致判断错误。
  5. 补充下锥方法:原代码缺少下锥的打印逻辑,补充后完整实现沙漏的完整图形。

内容的提问来源于stack exchange,提问作者Babette

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:13:33