我試圖找到具有十個輸入點的數組的最小值,但我以某種方式設法創建了只能找到最大值的東西。幫幫我?找到一個數組的最小值
import java.util.Scanner;
public class Ex7_9Smallestt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int count = 0;
System.out.print("Welcome to Elisabeth's smallest number finder!\n");
//print welcome message
double myList [] = new double[10]; //initialize array
while (count < 10) { //initialize for loop
//print enter a number and make that number an element in the array
int i = 0;
System.out.print("\nPlease enter a number:");
myList[i] = input.nextDouble();
count ++;
}
System.out.printf("The minimum is " + min(myList)); //print minimum
}
public static double min(double[] array) {
//create method to find lowest number
double minimum = array[0];
int i = 0;
//initialize for loop
for (double e : array) {
if (array[i] < minimum) {
minimum = array[i];
i++;
}
}
return minimum;
}
}
爲什麼在min方法中混合循環索引和集合?爲什麼不迭代數組並忘記索引? – ncmathsadist