我正在处理一项作业,我需要从 java 中的输入文件中打印 3 组不同分数的平均值。平均值必须四舍五入到小数点后 2 位。我尝试创建扫描仪,以便可以添加输入文件中的小数并求平均值,但是当我单击运行时,netbeans 只是运行并且没有打印出来。我也没有收到错误。任何关于我如何让它运行的提示将不胜感激。
输入文件内容:7.88 6.44 5.66 3.44 7.50 9.80 4.33 2.31 8.99 7.62 3.67 4.39
``这是代码(所有必要的导入都已导入)
public static void main(String[] args) throws IOException {
{
Scanner sc = new Scanner(new File("Gym.in"));
double Number = sc.nextFloat();
{
NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
Scanner sf = new Scanner(new File("Gym.in"));
int maxIndx = -1;
String text[] = new String[100];
while (sf.hasNext())
;
{
maxIndx++;
text[maxIndx] = sf.nextLine();
System.out.println(text[maxIndx]);
}
sf.close();
String answer;
double sum;
double average;
for (int j = 0; j <= maxIndx; j++) {
StringTokenizer st = new StringTokenizer(text[j]);
Scanner sg = new Scanner(text[j]);
System.out.println(text[j]);
Scanner ss = new Scanner(text[j]);
sum = 0;
average = sum / 10;
answer = "For Competitor #1, the average is: ";
while (sc.hasNext()) {
double i = sc.nextDouble();
answer = answer + i;
sum = sum + i;
}
answer = answer + average;
System.out.println(answer);
}
}
}
}