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 setisPricetotrueevery loop iteration, which is not what you want. You need a boolean check likewhile (isPrice)instead. - Array index out-of-bounds error: Your
arrarray has a length of 10 (indices 0-9), but your condition usesi <= 10. Whenihits 10, trying to accessarr[10]will crash the program. - Flawed minimum value initialization: Setting
min = 0means 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/elseblocks 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?
- Fixed loop condition: Used
while (keepRunning)(renamed the variable for clarity) to properly check if the loop should continue. - Safe min initialization:
Integer.MAX_VALUEensures the first valid number will correctly set the minimum, even if all valid inputs are negative. - Avoided array errors: Check
inputCount < 10before storing in the array, so we never access an index that doesn't exist. - Updated output: Now prints the first 10 numbers and minimum after input stops, matching your assignment requirements.
- Handled edge cases: Added a check for when the user enters
-1as the first input, so we don't output an incorrect minimum. - Cleaner structure: Simplified condition logic and renamed variables to make the code easier to read.
内容的提问来源于stack exchange,提问作者Michael Korn
相关产品推荐
相关产品推荐

