我想學習一些JAVA,目前我正在做一些基本的程序,我需要從文件中讀取然後找到MIN和MAX值,但在我的代碼中,我只能找到MAX值和MIN值不起作用。它只是顯示我分配的價值。有人能幫助我嗎?這是我的代碼到目前爲止。查找最小值和最大值JAVA
public class uzduotis {
public static void main(String[] args) {
int[] data = fileReading("skaiciai");
System.out.println(Arrays.toString(data));
}
// Reads numbers from file
public static int[] fileReading(String failas) {
int minVal = 0, maxVal = 0;
try {
File f = new File(failas);
Scanner s = new Scanner(f);
int ctr = 0;
while (s.hasNextInt()) {
ctr++;
s.nextInt();
}
int[] arr = new int[ctr];
Scanner s1 = new Scanner(f);
for (int i = 0; i < arr.length; i++) {
arr[i] = s1.nextInt();
maxVal = Math.max(maxVal, arr[i]);
minVal = Math.min(minVal, arr[i]);
}
System.out.println("Largest is " + maxVal);
System.out.println("Smallest is " + minVal);
return arr;
} catch (Exception e) {
return null;
}
}
}
學習調試的時間。 – rmlan