我想從用戶輸入的文件中找到浮點列表中最小的數字,而不使用任何數組,但我似乎無法弄清楚如何去做。這是我目前的,但它根本不起作用。任何幫助將不勝感激,請隨時編輯短語。如何在不使用任何數組的情況下找到浮點列表中的最小數字?
import java.io.File;
import java.util.Scanner;
public class Excercise3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file name: ");
String filename = sc.next();
File myFile = new File(filename);
String myString;
float incumbant = 0;
float secondPlace = 0;
float counter = 0;
try {
Scanner myScanner = new Scanner(myFile);
myScanner.nextLine();
//while(myScanner.hasNextFloat())
//{
counter = myScanner.nextFloat();
if (counter == 47.83) {
System.out.println("The top qualifier is: " + counter);
} else {
counter++;
}
//System.out.println(myString);
// }
myScanner.close();
sc.close();
} catch (Exception e) {
System.out.println("File not found.");
}
}
}
你看過可能的清單嗎? https://docs.oracle.com/javase/7/docs/api/java/util/List.html –
什麼是'incumbant'和'secondPlace'?我認爲你在正確的軌道上,但需要找出循環。 'if(counter == 47.83)'是什麼,這個數字從哪裏來? – Linus
你爲什麼要使用數組? – shmosel