我试图从用户输入中捕获最低的 double 值。我只捕获初始化的 min 变量的值 - 我错过了什么?谢谢!
public static void main(String[] args) {
double[] lowNum = new double[10];
Scanner input = new Scanner(System.in);
for (int i=0; i<=9; i++) {
System.out.println("Enter a double: ");
lowNum[i] = input.nextDouble();
}
input.close();
double min = calcLowNum(lowNum);
System.out.println(min);
}
public static double calcLowNum(double[] a) {
double min=0;
for (int i=0; i>=9; i++){
for (int j=0; j>=9; j++){
if (a[i]<=a[j] && j==9){
min=a[i];
}
else {
continue;
}
}
}
return min;
最佳答案
您可以使用 Collections#min找到最小值。不过,您将需要 Apache Commons-Lang
来实现此目的。
// This would be the array 'a'
double[] array = {15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Convert the primitive array into a Class array
Double[] dArray = ArrayUtils.toObject(array);
List<Double> dList = Arrays.asList(dArray);
// Find the minimum value
Double returnvalue = Collections.min(dList);
return returnvalue; // or you can do returnvalue.doubleValue() if you really want
关于java - 查找 Java 数组中最低的 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14611633/