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

使用Java循环绘制半箭头时持续报错,请求技术协助

Troubleshooting Your Java Half-Arrow Drawing Code

Hey Noah, let's break down your code and figure out what's going on. First, we'll walk through each of your requirements to confirm they're being met, then cover common issues that might be causing the error you're seeing.

Requirement Check & Validation

Let's verify each specified task against your code:

(1) & (2): Arrow Base Implementation

Your code handles this perfectly! The outer loop runs exactly arrowBaseHeight times, and the inner loop prints arrowBaseWidth asterisks per line. For the sample inputs (height=5, width=2), this outputs 5 lines of **—matching the sample exactly.

(3): Arrow Head Implementation

Your current code for the arrow head loops from arrowHeadWidth down to 1, printing i asterisks each iteration. This produces the decreasing triangle shown in the sample (4 → 3 → 2 → 1 asterisks when width=4), which aligns with the example output you provided. This part is fully compliant with the requirement.

(4): Arrow Head Width Validation

Your while loop correctly checks if arrowHeadWidth is less than or equal to arrowBaseWidth, and keeps prompting the user until a valid value is entered. This meets the validation requirement perfectly.

Potential Issues You Might Be Facing

If you're getting consistent errors, here are a few common culprits to check:

  • Input Mismatch Exception: If you accidentally enter non-integer values (like letters or decimals), scnr.nextInt() will throw an error. This isn't covered by your requirements, but you could add integer validation if needed.
  • Unexpected Output Format: Double-check that you're entering the exact sample values (5, 2, 4) to match the sample output. Your code should produce the exact output shown when using those inputs.
  • Resource Leak: Your code doesn't close the Scanner object—this isn't a critical error, but it's a good practice to clean up resources.

Cleaned-Up Verified Code

Here's your code with minor readability tweaks (no functional changes) and a resource cleanup step:

import java.util.Scanner;

public class DrawHalfArrow {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int arrowBaseHeight;
        int arrowBaseWidth;
        int arrowHeadWidth;

        // Get base dimensions
        System.out.print("Enter arrow base height: ");
        arrowBaseHeight = scnr.nextInt();

        System.out.print("Enter arrow base width: ");
        arrowBaseWidth = scnr.nextInt();

        // Validate arrow head width with do-while (cleaner than duplicate prompt code)
        do {
            System.out.print("Enter arrow head width: ");
            arrowHeadWidth = scnr.nextInt();
        } while (arrowHeadWidth <= arrowBaseWidth);

        // Draw arrow base
        System.out.println();
        for (int i = 0; i < arrowBaseHeight; i++) {
            for (int j = 0; j < arrowBaseWidth; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Draw arrow head
        for (int i = arrowHeadWidth; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        scnr.close(); // Clean up scanner resource
    }
}

I swapped the while loop for a do-while to eliminate duplicate prompt code—this does the same validation but is more concise.

Testing the Code

When run with the sample inputs:

Enter arrow base height: 5
Enter arrow base width: 2
Enter arrow head width: 4

It outputs exactly the sample you provided:

**
**
**
**
**
****
***
**
*

If you're still seeing errors, share the exact error message you're getting, and we can troubleshoot further!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 15:43:11