简体   繁体   English

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

[英]Error message: “identifier expected”

I´m doing an exercise from the learning book "Java, how to program". 我正在从学习书“ Java,如何编程”中进行练习。 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 the 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:39:期望的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:预期的System.out.printf(“汽车驾驶%.2f英里公升汽油\\ 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:System.out.printf类型的非法启动(“汽车驾驶%.2f英里公升汽油\\ n \\ n”,milPrLiter);
^ GasolineUsage.java:40: expected System.out.printf( "The car drives %.2f miles pr liter gasoline\\n\\n", milPrLiter ); ^ GasolineUsage.java:40:预期的System.out.printf(“汽车驾驶%.2f英里公升汽油\\ n \\ n”,milPrLiter);

^ GasolineUsage.java:42: class, interface, or enum expected } ^ 5 errors ^ GasolineUsage.java:42:预期的类,接口或枚举} ^ 5错误

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. 请注意,对于这种类型的错误,在抛出错误的行上方看总是很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM