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

我不明白为什么我的编译器找不到类,考虑到我不是已经在我的方法中做到了吗?编译器指向以下行: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);


    }

}



Qyouu
浏览 199回答 3
3回答

慕娘9325324

方法参数“maxWeight”的类型为“Weight”,Java无法找到解析程序依赖关系的定义。未能通过类路径找到类将导致错误“找不到符号”。您可能希望定义类“Weight”并将其导入,就像其他人对您所做的那样。你真的需要“重量”类吗?您只是将通过的论点与“警卫”的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);}

守候你守候我

您的代码中缺少权重类 请参阅以下代码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?trueOfficer, how about me?false**

翻过高山走不出你

你应该像这样定义类:Weightpublic class Weight {    public int weight;    public int getWeight() {        return weight;    }    public void setWeight(int weight) {        this.weight = weight;    }}输出:Officer, can I come in?trueOfficer, how about me?false或者使用关键字导入您的类。Weightimport
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java