首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >错误:应为'.class‘

错误:应为'.class‘
EN

Stack Overflow用户
提问于 2013-01-01 19:51:05
回答 4查看 37.7K关注 0票数 1

嗨,朋友们,编译下面的错误即将到来

错误:应为'.class‘

我找不到错误是什么。我查看了很多网站,但都不能找到为什么会出现这个错误。请帮助

提前谢谢。

代码语言:javascript
代码运行次数:0
运行
复制
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“来编译这个程序。

EN

回答 4

Stack Overflow用户

发布于 2013-01-01 19:58:15

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

childnames参数被定义为一个数组,因此您可以简单地传入参数replace:

代码语言:javascript
代码运行次数:0
运行
复制
t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);

使用

代码语言:javascript
代码运行次数:0
运行
复制
t.isGoodEmployee(ismarried,noofchild,middlename,childnames);

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

代码语言:javascript
代码运行次数:0
运行
复制
middlename.endssWith("e");
               ^---------extra 's'

熟悉javadocs

票数 4
EN

Stack Overflow用户

发布于 2013-01-01 19:58:00

这段代码有很多错误:

您已经在isGoodEmployee() (第1行)中声明了两次布尔子级

endssWith应为endsWith()

以此类推,但它们都不是您所说的问题。你使用什么命令行来编译这个类?

票数 1
EN

Stack Overflow用户

发布于 2013-01-01 20:50:40

@Reimeus已经做到了。

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

代码语言:javascript
代码运行次数:0
运行
复制
 t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14110171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档