我是新來的java,我試圖從我的文件打印我的唯一數字大於平均值。如何只打印大於平均值的數字?
3.2
7.5
1.11
9.4
平均:4.8525
double x;
Scanner inputStream = null;
try { // place file in project folder, not src;
inputStream = new Scanner(new File("Data.txt"));
} catch(FileNotFoundException e) {
System.out.println("Error opening file");
System.exit(1);
}
while(inputStream.hasNextDouble()){
x = inputStream.nextDouble();
System.out.println(x);
}
inputStream.close();
您開始寫代碼,甚至?打印之前有什麼條件的If條件? –
將值讀入列表中。根據列表中的值計算平均值。僅在列表中的項目大於平均值時纔打印列表中的項目。 – slipperyseal