0
import java.util.Scanner;

public class Testing {
public static void main(String[] args){
    int[] userInput = new int[8]; 
    int userDetermint,numEntered;

    Scanner csole = new Scanner(System.in);

    System.out.print("Choose how many numbers you wanted to input the limits is up to 9: ");
    userDetermint = csole.nextInt();
    System.out.println("Please Key In Your "+ userDetermint + " numbers: ");
    for (int i = 0; i < userDetermint; i++ ){
        numEntered = csole.nextInt();
        userInput[i]= numEntered;
    } 
    int smallest = userInput[0];
    int largest = userInput[0];
    for ( int index = 0; index < userInput.length; index++ ){
        if(userInput[index]>largest)
            largest  = userInput[index];
        else if (userInput[index]< smallest)
            smallest = userInput[index];
    }
    System.out.println("Largest Number is "+ largest);
    System.out.println("Smallest Number is "+ smallest);
}

}

通过查找用户从 2 到 10 输入的最大值和最小值来进行练习。我使用数组来精确定位它,但不知何故,最小值总是返回 0。想知道是否有人可以帮助我指出错误并解释它对我来说。

4

1 回答 1

2

这条线

for ( int index = 0; index < userInput.length; index++ ){

应该

for (int i = 0; i < userDetermint; i++ ){

否则您将进入该0区域。

于 2013-06-26T00:52:50.057 回答