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

Java代码运行报错求助:实现数字扫描、最小值及前10数输出

Fixing Your Java Input/Output Program

Hey there! Let's work through fixing your code together—there are a few small issues causing errors and unexpected behavior. Let's break down the problems first, then jump to the corrected version.

Key Issues in Your Original Code

  • Incorrect while loop condition: while (isPrice = true) is an assignment, not a comparison. This would accidentally set isPrice to true every loop iteration, which is not what you want. You need a boolean check like while (isPrice) instead.
  • Array index out-of-bounds error: Your arr array has a length of 10 (indices 0-9), but your condition uses i <= 10. When i hits 10, trying to access arr[10] will crash the program.
  • Flawed minimum value initialization: Setting min = 0 means if the first input is -1 (or all inputs are negative valid numbers), your min will stay 0, which is wrong. We should initialize it to a very large value instead.
  • Misaligned output logic: Your code prints values during input, but the requirement is to output the first 10 numbers after input stops.
  • Unnecessary condition branching: Some of your if/else blocks can be simplified to make the code cleaner.

Corrected Code

import java.util.Scanner;

public class Sheela1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean keepRunning = true;
        int inputCount = 0;
        int[] firstTenNumbers = new int[10];
        // Initialize min to the largest possible int value so first valid num replaces it
        int minimumValue = Integer.MAX_VALUE;

        while (keepRunning) {
            System.out.print("Enter a number (enter -1 or smaller to stop): ");
            int currentNum = scan.nextInt();

            // Handle stop condition first
            if (currentNum <= -1) {
                keepRunning = false;
                // Edge case: no valid numbers entered
                if (inputCount == 0) {
                    System.out.println("No valid numbers were input.");
                    scan.close();
                    return;
                }
                continue; // Skip rest of loop logic once stopping
            }

            // Update minimum value if current number is smaller
            if (currentNum < minimumValue) {
                minimumValue = currentNum;
            }

            // Store in firstTen array only if we haven't filled it yet
            if (inputCount < 10) {
                firstTenNumbers[inputCount] = currentNum;
            }

            inputCount++;
        }

        // Print the first 10 numbers (or all if fewer than 10 were entered)
        System.out.println("\nFirst 10 entered numbers:");
        int numbersToDisplay = Math.min(inputCount, 10);
        for (int i = 0; i < numbersToDisplay; i++) {
            System.out.printf("Number %d: %d%n", i + 1, firstTenNumbers[i]);
        }

        // Print the minimum value
        System.out.println("\nMinimum value entered: " + minimumValue);

        scan.close(); // Clean up the Scanner resource
    }
}

What Changed?

  1. Fixed loop condition: Used while (keepRunning) (renamed the variable for clarity) to properly check if the loop should continue.
  2. Safe min initialization: Integer.MAX_VALUE ensures the first valid number will correctly set the minimum, even if all valid inputs are negative.
  3. Avoided array errors: Check inputCount < 10 before storing in the array, so we never access an index that doesn't exist.
  4. Updated output: Now prints the first 10 numbers and minimum after input stops, matching your assignment requirements.
  5. Handled edge cases: Added a check for when the user enters -1 as the first input, so we don't output an incorrect minimum.
  6. Cleaner structure: Simplified condition logic and renamed variables to make the code easier to read.

内容的提问来源于stack exchange,提问作者Michael Korn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:40:34