为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复创建对象时的“错误:找不到符号”?

如何修复创建对象时的“错误:找不到符号”?

Qyouu 2022-09-07 16:29:15
我不明白为什么我的编译器找不到类,考虑到我不是已经在我的方法中做到了吗?编译器指向以下行:Weightpublic boolean checkWeight`Weight first = new Weight();`和`Weight second = new Weight();`public class Guard {    int stashWeight;    public Guard ( int maxWeight ) {        this.stashWeight = maxWeight;    }    public boolean checkWeight (Weight maxWeight) {        if ( maxWeight.weight >= stashWeight ) {            return true;        } else {            return false;        }    }    public static void main (String[] args ) {        Weight first = new Weight();        first.weight = 3000;        Weight second = new Weight();        second.weight = 120;        Guard grams = new Guard(450);        System.out.println("Officer, can I come in?");        boolean canFirstManGo = grams.checkWeight(first);        System.out.println(canFirstManGo);        System.out.println();        System.out.println("Officer, how about me?");        boolean canSecondManGo = grams.checkWeight(second);        System.out.println(canSecondManGo);    }}
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超5个赞

  1. 方法参数“maxWeight”的类型为“Weight”,Java无法找到解析程序依赖关系的定义。未能通过类路径找到类将导致错误“找不到符号”。您可能希望定义类“Weight”并将其导入,就像其他人对您所做的那样。

  2. 你真的需要“重量”类吗?您只是将通过的论点与“警卫”的stashWeight进行比较。

您的公共方法可以简单地是:


public boolean checkWeight(int maxWeight) {

        return maxWeight >= stashWeight;

}


主方法可以更新为:


public static void main(String[] args) {

    int firstWeight = 3000;

    int secondWeight = 120;


    Guard grams = new Guard(450);

    System.out.println("Officer, can I come in?");

    boolean canFirstManGo = grams.checkWeight(firstWeight);

    System.out.println(canFirstManGo);


    System.out.println();


    System.out.println("Officer, how about me?");

    boolean canSecondManGo = grams.checkWeight(secondWeight);

    System.out.println(canSecondManGo);


}


查看完整回答
反对 回复 2022-09-07
?
守候你守候我

TA贡献1802条经验 获得超10个赞

您的代码中缺少权重类 请参阅以下代码


class Weight {


    public int weight;


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}


public class Guard {


    int stashWeight;


    public Guard(int maxWeight) {

        this.stashWeight = maxWeight;

    }


    public boolean checkWeight(Weight maxWeight) {

        if (maxWeight.weight >= stashWeight) {

            return true;

        } else {

            return false;

        }

    }


    public static void main(String[] args) {

        Weight first = new Weight();

        first.weight = 3000;

        Weight second = new Weight();

        second.weight = 120;


        Guard grams = new Guard(450);

        System.out.println("Officer, can I come in?");

        boolean canFirstManGo = grams.checkWeight(first);

        System.out.println(canFirstManGo);


        System.out.println();


        System.out.println("Officer, how about me?");

        boolean canSecondManGo = grams.checkWeight(second);

        System.out.println(canSecondManGo);


    }

}

输出


**


Officer, can I come in?

true

Officer, how about me?

false

**


查看完整回答
反对 回复 2022-09-07
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

你应该像这样定义类:Weight


public class Weight {


    public int weight;


    public int getWeight() {

        return weight;

    }


    public void setWeight(int weight) {

        this.weight = weight;

    }

}

输出:


Officer, can I come in?

true


Officer, how about me?

false

或者使用关键字导入您的类。Weightimport


查看完整回答
反对 回复 2022-09-07
  • 3 回答
  • 0 关注
  • 198 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号