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

使用二维字符数组打印自定义填充字符正方形的代码求助

Fixing Your Square-Printing Java Code

Hey there! Let's get your square printer up and running smoothly. I noticed a couple of critical issues in your current code, plus some missing pieces to make it fully functional. Let's break down the fixes and walk through the complete working version.

First, let's spot the problems in your existing code:

  • Wrong array initialization: You wrote char[][] choice1 = new char[rowCol1][symbol1]; — using symbol1 (a char) as the column count will convert the character to its ASCII value, which is definitely not the square dimension you want. We need the column count to match the row count (rowCol1).
  • Incorrect character input: myKey.next(".") uses a regex to only match a dot (.), so it won't capture any other character the user enters. That's not what you need for a customizable fill character.
  • Incomplete loop: You only started the outer loop but didn't add the inner loop to fill the array or logic to print each row.
  • Missing input validation: The requirement specifies a 4-20 range for dimensions, but there's no check to ensure the user follows this.

Here's the fully corrected and functional code:

import java.util.Scanner;

public class SquarePrinter {
    public static void main(String[] args) {
        Scanner myKey = new Scanner(System.in);
        
        // Validate input for row/column count (4-20)
        int rowCol1;
        do {
            System.out.print("\nHow many rows and columns (min 4 & max 20)? ");
            rowCol1 = myKey.nextInt();
            if (rowCol1 < 4 || rowCol1 > 20) {
                System.out.println("Oops! That's not in range. Please enter a number between 4 and 20.");
            }
        } while (rowCol1 < 4 || rowCol1 > 20);
        
        // Capture the fill character (fixes the regex issue and handles leftover newline)
        System.out.print("Which character do you want to fill your square with? ");
        myKey.nextLine(); // Skip the newline left by nextInt()
        char symbol1 = myKey.nextLine().charAt(0);
        
        // Initialize a square 2D array (rows = columns)
        char[][] choice1 = new char[rowCol1][rowCol1];
        
        // Fill the array and print each row
        for (int i = 0; i < choice1.length; i++) {
            // Fill every position in the current row with the chosen character
            for (int j = 0; j < choice1[i].length; j++) {
                choice1[i][j] = symbol1;
            }
            // Convert the row's char array to a string and print it
            System.out.println(new String(choice1[i]));
        }
        
        myKey.close();
    }
}

Key fixes and improvements explained:

  1. Input validation: The do-while loop ensures the user can't proceed until they enter a valid number between 4 and 20, preventing invalid array sizes.
  2. Character input fix: myKey.nextLine() skips the newline character left in the input buffer after nextInt() (a common Java Scanner pitfall), and myKey.nextLine().charAt(0) captures the first character of whatever the user types (works for single characters or even longer strings, taking the first one).
  3. Proper array setup: new char[rowCol1][rowCol1] creates a true square array with matching rows and columns.
  4. Complete loop logic: The inner loop fills every index in the row with the chosen character, and we print each row immediately after filling it using new String(choice1[i]) (a clean way to convert a char array to a printable string).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:28:19