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

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

  1. Program won't terminate when entering 0: The biggest problem is that inside your while loop, you're calling in.nextInt() three times per iteration (once in the loop condition, then two more for small and large). 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.
  2. Incorrect min/max tracking: You initialized small and large to 0, which will skew results if all inputs are positive or negative. Plus, the extra nextInt() calls meant you weren't actually tracking the input value you just read.
  3. Odd/even count issues: While using double for counts works, it's more logical to use int (since counts are whole numbers). More importantly, the extra input reads meant most of your entries weren't even being counted for parity.
  4. 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 largest and smallest to the first valid input, ensuring accurate tracking regardless of input range.
  • Improved count variables: Switched even/odd to int types for clarity, and now every input is correctly counted for parity.
  • Prevented overflow: Used long for sum to handle cases where adding many large integers would exceed the limit of int.
  • Cleaner average output: Used printf to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:48:49