Как исправить ошибку «Ошибка: не удается найти символ» при создании объекта? - PullRequest
1 голос
/ 27 марта 2019

Я не понимаю, как мой компилятор не может найти класс Weight, учитывая, что я уже не сделал это в моем методе public 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);

    }
}

Ответы [ 4 ]

0 голосов
/ 27 марта 2019
  1. Параметр метода "maxWeight" имеет тип "Weight", для которого Java не может найти определение для разрешения зависимостей вашей программы. Невозможность найти класс через classpath приведет к ошибке «Не удается найти символ». Возможно, вы захотите определить класс «Вес» и импортировать его, как другие люди ответили вам.
  2. Вам действительно нужен класс "Вес"? Вы просто сравниваете переданный аргумент с stashWeight "Guard's".

Ваш публичный метод может быть просто:

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);

}
0 голосов
/ 27 марта 2019

ваш весовой класс отсутствует в вашем коде. См. Код ниже

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

**

0 голосов
/ 27 марта 2019

Ваш весовой класс находится в другой упаковке? Что такое модификатор доступа класса Weight?

Пожалуйста, проверьте выше и посмотрите ниже документ.

Модификаторы доступа

0 голосов
/ 27 марта 2019

Вы должны определить класс 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

Или импортировать класс Weight, используя ключевое слово import.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...