错误消息:“标识符预期”

时间:2011-09-24 19:51:17

标签: java

我正在从学习书“Java,如何编程”中进行练习。我本来应该编写一个程序,让用户输入里程数,以及用于未确定行程次数的汽油升。

该程序应该为每次旅行打印公升里程,最后打印平均里程数。

我应该使用哨兵控制的重复,并将里程数打印为浮点值。

我在尝试编译时不断收到错误消息:

  

GasolineUsage.java:39:预计                  milPrLiter =(double)totalMil / totalLiters;                            ^ GasolineUsage.java:40:   预期                  System.out.printf(“汽车驱动%。2英里每公升   汽油\ n \ n“,milPrLiter);
                                  ^ GasolineUsage.java:40:非法开始   类型                  System.out.printf(“汽车驱动%。2英里每公升   汽油\ n \ n“,milPrLiter);
                                    ^ GasolineUsage.java:40:    预期                  System.out.printf(“汽车驱动%。2英里每公升   汽油\ n \ n“,milPrLiter);

     

^ GasolineUsage.java:42:期望的类,接口或枚举} ^ 5   错误

代码如下:

import java.util.Scanner;
public class GasolineUsage
{

    public static void main( String[] args )
    {    
        int totalMil = 0;
        int totalLiters = 0;
        double milPrLiter1 = 0;
        double milPrLiter;

        Scanner inputMil = new Scanner(System.in);
        Scanner inputLiters = new Scanner( System.in);

        System.out.print( "Type in miles first trip, to end type: -1 " );
            int mil = inputMil.nextInt();
        System.out.print( "Type in liters of gasoline used on first trip " );
            int liters = inputLiters.nextInt();
            totalMil = totalMil + mil; 
            totalLiters = totalLiters + liters;
        milPrLiter1 = (double) mil / liters;
        System.out.printf( "(%.2f miles pr liter this trip.)\n", milPrLiter1 );

        System.out.print( "Type in miles on next trip " );
            while (mil != -1)
                {
                 mil = inputMil.nextInt();
                     if (mil != -1)
                         {totalMil = totalMil+ mil;

                         System.out.print( "Type in lters used on next trip: " );
                         liters = inputLiters.nextInt();}
                         totalLiters = totalLiters + liters;

                         milPrLiter1 = (double) mil / liters;
                         System.out.printf( "(%.2f miles pr liter this trip.)\n", milPrLiter1 );}
                 }

               milPrLiter = (double) totalMil / totalLiters;
               System.out.printf( "The car drives %.2f miles pr liter gasoline\n\n", milPrLiter );               
    }
} 

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

计算你应该匹配的开始和结束括号。您的代码中隐藏了一个额外的右括号,如果您搜索它,您将找到它。请注意,对于这种类型的错误,最好先查看引发错误的行。