Java对话框无法完整显示数组内容问题求助
Hey there! I spot the issue right away—your current displayCounts method is only calculating the total number of random numbers generated, not building out the breakdown of individual counts you want to show. Let's fix that so you can see each number's occurrence clearly in the dialog.
The Problem with Your Current Code
In your displayCounts method, you’re using an int variable message that just sums up all the counts. When you pass that to JOptionPane.showMessageDialog, it only shows the total, not the per-number stats you need. To display a table, you need to build a formatted string that lists each number and its corresponding count.
Modified displayCounts Method
Here’s how to rewrite the method to create a clean, readable table for the dialog:
public static void displayCounts(int[] counts) { // Use StringBuilder to efficiently build our multi-line message StringBuilder message = new StringBuilder(); message.append("Number\tCount\n"); // Header row with tab for alignment message.append("----------------\n"); // Divider line for clarity // Loop through counts to map each index to the correct number (1-10) for (int i = 0; i < counts.length; i++) { int number = i + 1; // counts[0] tracks number 1, so offset by 1 message.append(number).append("\t").append(counts[i]).append("\n"); } // Show the formatted table in the dialog JOptionPane.showMessageDialog( null, message.toString(), "Random Number Counts", JOptionPane.INFORMATION_MESSAGE ); }
Key Changes Explained
- StringBuilder instead of int: We use
StringBuilderto efficiently construct our multi-line message (it’s more performant than concatenating strings with+in a loop). - Structured table format: Added a header row, divider, and tabs (
\t) to align numbers and counts for a neat, easy-to-read layout. - Correct number mapping: Since
counts[0]corresponds to the number 1, we calculatenumber = i + 1to match each count to its actual value.
Extra Polish Tips
- For even cleaner alignment, replace the tab with
String.formatto pad numbers to a fixed width:
This left-aligns the number in a 6-character space, making all counts line up perfectly.message.append(String.format("%-6d %d\n", number, counts[i])); - Add a check in your input handling to ensure
nis a positive number (right now, entering 0 generates an empty array, which works but might not be what you intend):try { n = Integer.parseInt(temp); if (n <= 0) { title = "Please enter a positive number!"; ok = false; } } catch (Exception e) { // Existing error handling }
Full Working Code (Optional)
If you want the complete updated program with all fixes and improvements, here it is:
import javax.swing.JOptionPane; public class RandomArray { public static void main(String[] args) { String title, temp; boolean ok; int n = 0; title = "Random Number Generator With Counts"; do { ok = true; temp = JOptionPane.showInputDialog(null, "Enter amount of random numbers to generate: ", title, JOptionPane.QUESTION_MESSAGE); // Handle user clicking Cancel if (temp == null) { JOptionPane.showMessageDialog(null, "Program canceled.", "Info", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } try { n = Integer.parseInt(temp); if (n <= 0) { title = "Please enter a positive number!"; ok = false; } } catch (Exception e){ title = temp + " is not valid!"; ok = false; } } while(!ok); int[] numbers = createArray(n); int[] counts = countNumbers(numbers); displayCounts(counts); } public static int[] createArray(int n) { int[] numbers = new int[n]; int low = 1, high = 10; for(int i = 0; i < numbers.length; i++) { numbers[i] = (int)(Math.random()*high) + low; } return numbers; } public static int[] countNumbers(int[] numbers) { int[] counts = new int[10]; for(int i = 0; i < numbers.length; i++) { counts[numbers[i]-1]++; } return counts; } public static void displayCounts(int[] counts) { StringBuilder message = new StringBuilder(); message.append("Number\tCount\n"); message.append("----------------\n"); for (int i = 0; i < counts.length; i++) { int number = i + 1; message.append(number).append("\t").append(counts[i]).append("\n"); } JOptionPane.showMessageDialog( null, message.toString(), "Random Number Counts", JOptionPane.INFORMATION_MESSAGE ); } }
This will now show a clear, organized table of each number's occurrence in the dialog box.
内容的提问来源于stack exchange,提问作者mo-mouse

