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
答案 0 :(得分:1)
在实际输入myArray值之前,您已将min
和Max
分配给myArray[0]
。
由于myArray
的默认值为零,因此分配为零。</ p>