开发者

Error message: "identifier expected"

I´m doing an exercise from the learning book "Java, how to program". I am supposed to write a program that makes the user type in miles driven, and liters of gasoline used for a not decided number of trips.

The program is supposed to print miles pr liter for each trip, and an average miles pr liter at the end.

I am supposed to use sentinel-controlled repetition, and to print t开发者_Go百科he miles pr liter as floating point values.

I keep getting an error message when I try to compile:

GasolineUsage.java:39: expected milPrLiter = (double) totalMil / totalLiters; ^ GasolineUsage.java:40: expected System.out.printf( "The car drives %.2f miles pr liter gasoline\n\n", milPrLiter );

^ GasolineUsage.java:40: illegal start of type System.out.printf( "The car drives %.2f miles pr liter gasoline\n\n", milPrLiter );

^ GasolineUsage.java:40: expected System.out.printf( "The car drives %.2f miles pr liter gasoline\n\n", milPrLiter );

^ GasolineUsage.java:42: class, interface, or enum expected } ^ 5 errors

The code is the following:

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 );               
    }
} 

Can anyone please help me?


Count your opening and closing braces as they should match. You've got an extra closing brace hiding in your code that you'll find if you search for it. Note that with this type of error it's always good to look above the line that is throwing the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜