我的代码有什么问题? MIN没有显示

时间:2018-01-26 19:13:30

标签: java arrays

import java.util.Scanner;

public class MaxMin {

    public static void main(String[] args) {

        Scanner myScanner = new Scanner(System.in);

        System.out.println("Please enter the arrays value : ");
        int userInput = myScanner.nextInt();

        int[] myArray = new int[userInput];

        System.out.println("please enter the values for arrays : ");

        int sum = 0;
        int max = myArray[0];
        int min = myArray[0];

        for (int i = 0; i < myArray.length; i++) {
            myArray[i] = myScanner.nextInt();
            sum = sum + myArray[i];
        }

        for (int i = 1; i < myArray.length; i++) {
            if (max < myArray[i]) {
                max = myArray[i];
            }

            if (min > myArray[i]) {
                min = myArray[i];
            }
        }

        System.out.println("The sum is : " + sum + " \nmax is : " + max + "\nmin is : " + min);
    }
}

OUTPUT ------------------------------

请输入数组值: 五 请输入数组的值: 五 63 23 58 6 总和是:155 最大值是:63 min是:0 //为什么0

1 个答案:

答案 0 :(得分:1)

在实际输入myArray值之前,您已将minMax分配给myArray[0]

由于myArray的默认值为零,因此分配为零。<​​/ p>