2 回答

TA貢獻1848條經驗 獲得超10個贊
一種方法是使用Integer可以接受字符串輸入的類中的構造函數:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int sum = 0;
int cnt = 0;
try (BufferedReader br = Files.newBufferedReader(Paths.get(path+filename))) {
String line;
while ((line = br.readLine()) != null) {
Integer val = new Integer(line);
if (val < min) min = val;
if (val > max) max = val;
sum += val;
++cnt;
}
}
catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
System.out.println("average value: " + (sum / cnt));
System.out.println("highest value: " + max);
System.out.println("smallest value: " + min);
添加回答
舉報