数字分析程序开发求助:实现多维度统计与直方图功能
Hey there! Let's break down what's going on with your code and get you on track to build that useful number analysis program. First, let's fix the core issue of counting valid numbers, then expand to all the analysis features you need.
Your current code has a few small bugs that prevent it from correctly tracking input numbers:
- The
xvariable never increments, so every input overwrites the first position in the array - You're storing the sentinel value
0(plus 1, which muddles the data) instead of excluding it - When printing, you loop through the full 50-length array, most of which are default
0s, making it look like only0was counted
Here's a revised input section that correctly collects valid 1-50 numbers and tracks their count:
import java.util.Scanner; public class Analysis { // Fixed the class name spelling (was Analasys) public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] numbers = new int[50]; // Array to store valid inputs int validCount = 0; // Tracks how many valid numbers we've collected System.out.println("Enter numbers between 1-50, type 0 to quit."); while (true) { int input = scan.nextInt(); // Exit loop if user enters 0 if (input == 0) { break; } // Only store numbers in the valid range if (input >= 1 && input <= 50) { numbers[validCount] = input; validCount++; // Increment count each time we add a valid number } else { System.out.println("Oops! Please enter a number between 1 and 50."); } } // Now we can see the total valid numbers System.out.println("\nTotal valid numbers entered: " + validCount); // Optional: Print all collected numbers System.out.print("Your numbers: "); for (int i = 0; i < validCount; i++) { System.out.print(numbers[i] + " "); } scan.close(); } }
With valid numbers stored and counted, let's build out each analysis task one by one:
Calculate Average
Use a sum of all valid numbers, then divide by the count (use double to avoid integer division):
if (validCount > 0) { int sum = 0; for (int i = 0; i < validCount; i++) { sum += numbers[i]; } double average = (double) sum / validCount; System.out.printf("\nAverage: %.2f", average); }
Find Maximum Value
Loop through the valid numbers to track the largest one:
if (validCount > 0) { int max = numbers[0]; for (int i = 1; i < validCount; i++) { if (numbers[i] > max) { max = numbers[i]; } } System.out.println("\nMaximum value: " + max); }
Calculate Range (Max - Min)
First find the minimum value, then subtract it from the maximum:
if (validCount > 0) { int min = numbers[0]; for (int i = 1; i < validCount; i++) { if (numbers[i] < min) { min = numbers[i]; } } int range = max - min; // Reuse the max variable from above System.out.println("Range (max - min): " + range); }
Find Mode (Most Frequent Number)
You mentioned "median" but noted it should be mode—we'll count occurrences of each number to find the most common one:
if (validCount > 0) { int[] frequency = new int[51]; // Indexes 1-50 match our number range for (int i = 0; i < validCount; i++) { frequency[numbers[i]]++; } int mode = 1; int highestFrequency = frequency[1]; for (int i = 2; i <= 50; i++) { if (frequency[i] > highestFrequency) { highestFrequency = frequency[i]; mode = i; } } System.out.println("Mode (most frequent number): " + mode + " (appeared " + highestFrequency + " times)"); }
Display Histogram (5-Unit Intervals)
Split 1-50 into 10 intervals, count numbers per interval, then print asterisks for each occurrence:
if (validCount > 0) { int[] intervalCounts = new int[10]; // 10 intervals total (1-5, 6-10, ..., 46-50) for (int i = 0; i < validCount; i++) { int num = numbers[i]; int intervalIndex = (num - 1) / 5; // Maps number to its interval index intervalCounts[intervalIndex]++; } System.out.println("\nHistogram (each * = 1 number):"); for (int i = 0; i < 10; i++) { int start = i * 5 + 1; int end = start + 4; System.out.printf("%2d-%2d: ", start, end); for (int j = 0; j < intervalCounts[i]; j++) { System.out.print("*"); } System.out.println(); } }
Make sure to add checks for validCount > 0 before running analysis (to avoid errors if the user enters 0 immediately). You can combine all these snippets into a single program for a complete solution.
内容的提问来源于stack exchange,提问作者calvin jackson

