使用二维字符数组打印自定义填充字符正方形的代码求助
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];— usingsymbol1(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:
- Input validation: The
do-whileloop ensures the user can't proceed until they enter a valid number between 4 and 20, preventing invalid array sizes. - Character input fix:
myKey.nextLine()skips the newline character left in the input buffer afternextInt()(a common Java Scanner pitfall), andmyKey.nextLine().charAt(0)captures the first character of whatever the user types (works for single characters or even longer strings, taking the first one). - Proper array setup:
new char[rowCol1][rowCol1]creates a true square array with matching rows and columns. - 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
相关产品推荐
相关产品推荐

