我一直把零作为最小值。我知道它与初始化为零的最小值有关,但我无法绕过它并正确地改变。
我的代码:
import java.util.Scanner;
import java.util.Arrays;
public class InputPractice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float average;
int NumberofIntegers = 0;
double total = 0;
System.out.println("Please enter the number of Integers: ");
NumberofIntegers = input.nextInt();
for (int i = 1; i <= NumberofIntegers; i++) {
System.out.print("Entered Value " + i + " : ");
total += input.nextDouble();
}
int minValue = 0;
int maxValue = 0;
for (int i = 1; i <= NumberofIntegers; i++) {
System.out.print("Enter value number " + i + " again: ");
int inputNumbers = input.nextInt();
if (inputNumbers > maxValue) {
maxValue = inputNumbers;
} else if (inputNumbers < minValue){
minValue = inputNumbers;
}
}
System.out.println("Maximum Value: " + maxValue);
System.out.println("Minimum Value: " + minValue);
System.out.println("Average : " + (total / NumberofIntegers));
}
}
答案 0 :(得分:2)
错误的初始化。简单地说 - 0总是小于你的任何数字。
MIN必须为MAX_VALUE,而MAX必须为MIN_VALUE(正数为0)
OR
您可以使用第一个输入数作为起始MIN和MAX。