Java输入统计程序故障排查:无法终止、奇偶统计错误等问题
Fixing Your Integer Statistics Program
Let's walk through the bugs in your code and fix them to get the program working as expected:
Key Issues in the Original Code
- Program won't terminate when entering 0: The biggest problem is that inside your
whileloop, you're callingin.nextInt()three times per iteration (once in the loop condition, then two more forsmallandlarge). This means when you enter 0, the program is still waiting for two more inputs before it checks the loop condition again—so it never exits properly. - Incorrect min/max tracking: You initialized
smallandlargeto 0, which will skew results if all inputs are positive or negative. Plus, the extranextInt()calls meant you weren't actually tracking the input value you just read. - Odd/even count issues: While using
doublefor counts works, it's more logical to useint(since counts are whole numbers). More importantly, the extra input reads meant most of your entries weren't even being counted for parity. - Edge case for no input: The original code should handle this, but the other bugs prevented it from working correctly.
Fixed Code
import java.util.Scanner; public class Lab4 { public static void main(String[] args) { int counter = 0; int evenCount = 0; int oddCount = 0; long sum = 0; // Use long to avoid integer overflow for large sums int input; int largest = 0; int smallest = 0; double average; System.out.print("Enter a series of values (0 to quit): "); Scanner in = new Scanner(System.in); while ((input = in.nextInt()) != 0) { sum += input; counter++; // Initialize min/max with the first valid input if (counter == 1) { largest = input; smallest = input; } else { if (input > largest) { largest = input; } if (input < smallest) { smallest = input; } } // Count even/odd if (input % 2 == 0) { evenCount++; } else { oddCount++; } } if (counter > 0) { average = (double) sum / counter; System.out.println("The smallest integer is: " + smallest); System.out.println("The largest integer is: " + largest); System.out.println("Total number of integers entered is " + counter); System.out.println("Total even numbers entered is " + evenCount); System.out.println("Total odd numbers entered is " + oddCount); System.out.printf("The average value is: %.2f%n", average); // Format for readability } else { System.out.println("No data was entered."); } in.close(); // Don't forget to close the scanner! } }
What Changed?
- Fixed loop input handling: We now only read one input per loop iteration, so entering 0 immediately triggers the loop exit.
- Proper min/max initialization: We set
largestandsmallestto the first valid input, ensuring accurate tracking regardless of input range. - Improved count variables: Switched
even/oddtointtypes for clarity, and now every input is correctly counted for parity. - Prevented overflow: Used
longforsumto handle cases where adding many large integers would exceed the limit ofint. - Cleaner average output: Used
printfto format the average to 2 decimal places for better readability. - Closed the scanner: Added
in.close()to follow best practices for resource management.
Now the program will:
- Terminate immediately when you enter 0
- Correctly count even/odd numbers
- Accurately track the smallest and largest inputs
- Calculate the sum and average correctly
- Show "No data was entered" if you input 0 right away
内容的提问来源于stack exchange,提问作者Sharon Nystrom
相关产品推荐
相关产品推荐

