【问题标题】:error: '.class' expected [duplicate]错误:“.class”预期[重复]
【发布时间】:2013-01-01 11:51:04
【问题描述】:

各位朋友,编译时出现以下错误

错误:预期为“.class”

我无法找到错误所在。我检查了许多网站,但无法找到此错误出现的原因。请帮忙

提前致谢。

import java.io.*;

class Test
{
    boolean isGoodEmployee(boolean ismarried,int noofchild,String middlename,String childnames[])
    {
        boolean child,letter,last,child;

        if (noofchild <= 2)
            child=true;
        boolean firstletter = middlename.startsWith("k");
        boolean lastletter = middlename.endssWith("e");

        if (firstletter && (!lastletter))
            letter = true;

        int lastnameletterscount = lastname.length();

        if (lastnameletterscount > 4)
            last = true;

        String name = raju;

        for (int i=0; i < noofchild; i++)
            if(name.equalsIgnoreCase(childnames[i]))
            {
                child = true;
                break;
            }
    }
}


class GoodEmployee
{
    public static void main(String args[]) throws IOException
    {
        String firstname, middlename, lastname;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the first name of employee");
        firstname = br.readLine();
        System.out.println("enter the middle name of employee");
        middlename = br.readLine();
        System.out.println("enter the last name of employee");
        lastname = br.readLine();
        //System.out.println("full name of employee is:"+first+middle+last);
        System.out.println("enter employee is married or not");
        boolean ismarried = Boolean.parseBoolean(br.readLine());

        if (ismarried)
            System.out.println("enter number of childrens");

        int noofchild = Integer.parseInt(br.readLine());
        String childnames[] = new String[noofchild];
        System.out.println("enter children names");

        for (int i=0; i < noofchild; i++)
            childnames[i] = br.readLine();
        Test t = new Test();
        t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
    }
}

我正在使用“javac GoodEmployee.java”来编译程序

【问题讨论】:

  • 请缩进你的代码,很难阅读!!
  • 请输出完整的错误
  • 请在您的问题中包含您用于编译文件的确切命令。你不是例如使用java 而不是javac 来编译,对吧?
  • @StephenC :您如何将这个(大约 5 年前被问到)标记为 3 天前添加的问题。我认为应该是重复的。在标记和突出您的问题之前,请检查询问的日期
  • 在将您的问题标记为重复之前,我确实检查了它。日期无关

标签: java class


【解决方案1】:

isGoodEmployee 中有一个不匹配的方法调用。

childnames 参数被定义为一个数组,所以你可以简单地传入参数,替换:

t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);

t.isGoodEmployee(ismarried,noofchild,middlename,childnames);

除此之外,不要忽略其他编译器错误,例如

middlename.endssWith("e");
               ^---------extra 's'

熟悉javadocs

【讨论】:

    【解决方案2】:

    这段代码有很多问题:
    您已在 isGoodEmployee() (第一行)中声明了两次布尔孩子
    endsWith 应该是 endsWith()
    等等,但不是你所说的问题。你用什么命令行来编译这个类?

    【讨论】:

      【解决方案3】:

      @Reimeus 成功了。

      奇怪的编译错误可以解释如下。在这一行:

       t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
      

      childnames[] 短语应该是 Java 表达式。但事实上,语法最接近于一个类型......实际上是一个名为childnames 的(不存在的)类的数组。 Java 编译器的语法错误恢复代码已经决定,如果它在类型名(即childnames[].class)之后插入.class,将给出一个语法上有效的表达式。

      当然,编译器建议的更正是荒谬的......尤其是因为在您的应用程序中不会有一个名为 childnames 的类或接口。


      这个故事的寓意是,不寻常的语法错误有时会导致编译器产生特殊的错误消息,只有在您能够弄清楚解析器如何解析错误输入时,这些错误消息才有意义。

      【讨论】:

        【解决方案4】:

        如果您使用的是 IDE,则将具有 main 方法的类公开。 如果您尝试使用命令窗口运行它,我认为编译不需要“.class”文件,而是需要执行。因此,在您通过输入 javac GoodEmployee 获得 .class 文件并且没有错误后输入 java GoodEmployee ,它将执行您拥有的 .class 文件。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-19
          相关资源
          最近更新 更多