这就是我所拥有的:

  double[] miles = new double [10];

  double milesPerWeek = 26.0 / 10;
  double totalMiles = 0;
  double sum = 0;
  double average = 0;

  System.out.println("Week\tMiles");

  for (int i = 0; i < miles.length; i++){

     miles[i] += milesPerWeek;
     totalMiles += miles[i];

     System.out.printf("Week %d\t%.1f%n", i + 1, totalMiles);
  }
  for (int i = 0; i < miles.length; i++)
  {
     sum = sum + miles[i];
     average = sum / miles.length;
  }
  System.out.printf("The total of miles run is: %.1f%n" + sum + "\n");

  System.out.printf("The average of miles run is: %.1f%n" + average);

这是我求和和平均值的问题。如果没有此错误消息,我似乎无法使用print f格式化小数位:
运行的总里程数是:线程“主”中的异常java.util.MissingFormatArgumentException:格式说明符'%.1f'

您能指导我吗,因为我需要一位小数。

最佳答案

您需要将值作为参数传递,而不是连接它们:

System.out.printf("The total of miles run is: %.1f%n\n", sum);

System.out.printf("The average of miles run is: %.1f%n", average);

10-06 15:08