如何查找數組中的最小數字?我的代碼的問題是它總是打印出0作爲最小的數字。數組中最小的數字
這裏是我的代碼:
import java.util.Scanner;
public class Exercise1 {
public static void main (String [] args){
Scanner kb = new Scanner(System.in);
System.out.print("Please type the total number of marks: ");
int SIZE = kb.nextInt();
double [] marks = new double [SIZE];
double smallest = marks [0];
for (int i=0;i<SIZE;i++){
System.out.print("Enter the mark: ");
marks[i]=kb.nextDouble();
if(marks[i] < smallest) {
smallest = marks[i];
}
}
System.out.println("The lowest number is " + smallest);
}
}
爲什麼你的代碼打印0以外的任何東西? –
想一想......在'double smallest = marks'[0];'處標記'smallest'的值是多少,然後'if'(marks [i]
@Sortirios:因爲用戶輸入負數... –