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

C#空心正方形绘制逻辑修正:解决误触发无法绘制提示问题

Fixing the "cannot draw the shape" Error in Hollow Square Drawing

It looks like your current hollow square drawing logic is incorrectly rejecting valid dimensions (like height=1, width=5) because the condition check and loop handling aren’t accounting for edge cases where one dimension is exactly 1 but still valid. Let’s fix that by adjusting the validation condition and adding proper handling for single-row or single-column squares.

Corrected Code Snippet

Here’s the updated DrawHollowSquare method for your ShapeDrawer class:

using System;
using System.Text;

public class ShapeDrawer {
    public void DrawHollowSquare(int height, int width) {
        // Only show error if either dimension is less than 1 (invalid)
        if (height < 1 || width < 1) {
            Console.WriteLine("cannot draw the shape");
            return;
        }

        // Handle single-row square (height = 1)
        if (height == 1) {
            Console.WriteLine(new string('*', width));
            return;
        }

        // Handle single-column square (width = 1)
        if (width == 1) {
            for (int i = 0; i < height; i++) {
                Console.WriteLine("*");
            }
            return;
        }

        // Draw normal hollow square (height >=2 and width >=2)
        for (int i = 0; i < height; i++) {
            StringBuilder line = new StringBuilder();
            for (int j = 0; j < width; j++) {
                // Print star if it's the first/last row or first/last column
                if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
                    line.Append("*");
                } else {
                    line.Append(" ");
                }
            }
            Console.WriteLine(line.ToString());
        }
    }
}

Key Changes Explained

  1. Validation Condition: The check now only triggers the error if height < 1 OR width < 1—this allows dimensions equal to 1, which are valid (a 1x5 square is just a horizontal line of stars, and a 5x1 is a vertical line).
  2. Single-Row Handling: Added a specific case for when height == 1 to print a single line of stars (since there’s no "inner" space to fill).
  3. Single-Column Handling: Similarly, added a case for width == 1 to print vertical stars, which is a valid 1-column hollow square.
  4. Loop Adjustments: The main loops now run only when both dimensions are >=2, ensuring the hollow pattern works correctly without errors.

Testing Edge Cases

You can test these scenarios to confirm the fix works:

  • DrawHollowSquare(1,5): Outputs ***** (no error)
  • DrawHollowSquare(5,1): Outputs 5 lines each with * (no error)
  • DrawHollowSquare(0,5): Outputs "cannot draw the shape" (correct, invalid height)
  • DrawHollowSquare(3,4): Outputs a proper 3x4 hollow square

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:51:56