所以我试图得到一个读取文件的程序,文件的长度作为第一行。然后在文件中找到最小值和最大值的索引。烦人的是,由于某种原因,我的 findMin 和 findMax 方法不起作用。似乎他们绕过了 for 循环。我确定我只是犯了一个愚蠢的错误,但我似乎无法确定它。
import java.io.File;
import java.util.Scanner;
public class Driver00 {
public static int minpos;
public static int maxpos;
public static double min;
public static double max;
public static int numitems;
public static void main(String[] args) throws Exception {
Scanner infile = new Scanner(new File("data.txt"));
int numitems = infile.nextInt();
double[] array = new double[numitems];
for (int k = 0; k < numitems; k++) {
array[k] = infile.nextDouble();
}
infile.close();
int minPos, maxPos;
minPos = findMin(array);
maxPos = findMax(array);
System.out.println("Minimum value: " + minPos);
System.out.println("Maximum value: " + maxPos);
}
private static int findMin(double[] apple) {
for (int x = 0; x < numitems; x++)
// not activating
if (x == 1) {
if (apple[x] < apple[x - 1]) {
min = apple[x];
minpos = x;
} else {
min = apple[x - 1];
minpos = x - 1;
}
} else {
if (apple[x] < min) {
min = apple[x];
minpos = x;
}
}
return minpos;
}
private static int findMax(double[] banana) {
for (int x = 0; x < numitems; x++)
// not activating
if (x == 1) {
if (banana[x] > banana[x - 1]) {
max = banana[x];
maxpos = x;
} else {
max = banana[x - 1];
maxpos = x - 1;
}
} else {
if (banana[x] > max) {
max = banana[x];
maxpos = x;
}
}
return maxpos;
}
}