-omitted code-
{
while(miles != -999)
{
System.out.print("Enter the numbers of gallons purchased: ");
gallons = input.nextDouble();
totalmpg = (totalmiles / totalgallons);
totalgallons = totalgallons + gallons;
System.out.printf("Your miles per gallon this tank was:%.2f\n ", mpgthistank);
mpgthistank = miles / gallons;
System.out.printf("Your total miles driven is:%.2f\n ", totalmiles);
totalmiles = totalmiles + miles;
System.out.printf("Your total gallons used is:%.2f\n: ", totalgallons);
totalgallons = totalgallons + gallons;
System.out.printf("Your total miles per gallon is:%.2f\n ", totalmpg);
totalmpg = totalmiles / totalgallons;
System.out.print("Enter the number of miles traveled<-999 to quit>: ");
miles = input.nextDouble();
}
不完全确定为什么。这是我得到的运行:
Enter miles driven since last full tank <-999 to quit>: 100
Enter the numbers of gallons purchased: 10
Your miles per gallon this tank was:0.00
Your total miles driven is:0.00
Your total gallons used is:10.00
Your total miles per gallon is:NaN
Enter the number of miles traveled<-999 to quit>:
But it should read:
Your miles per gallon this tank was: 10
Your total gallons used is: 10
Your total miles per gallon is: 10
...然后循环应该重新开始(它确实如此)。我确信这是一个简单的语法错误,但我无法指出错误。