ImpressiveNickname
@ImpressiveNickname
Учу Java)))

Почему этот код не работает?

public class People {
    private String eyecolor;
    private String gender;
    
    People() {
        this.setEyecolor("Blue");
    }
    
    People(String x) {
        this.setEyecolor(x);
    }
    
    People() {
        this.setGender("male");
    }
    
    People(String d) {
        this.setGender(d);
    }
    
    public void setEyecolor(String x) {
        this.eyecolor = x;
    }
    
    public String getEyecolor() {
        return eyecolor;
    }
    
    public String getGender() {
        return gender;
    }
    
    public void setGender(String d) {
        this.gender = d;
    }
}

class Myclass {
    public static void main(String [] args) {
        People Mary = new People();
        People Joe = new People("Brown");
        Mary.setGender("female");
        Joe.setGender("male");
        OMG(Joe);
    }
    public static void OMG(People h) {
        if(h.getGender("male")) {
            System.out.println("OMG, look at his beautiful " + getEyecolor() + " eyes!");
        }
        else if(h.getGender("female")) {
            System.out.println("OMG, look at her beautiful " + getEyecolor() + " eyes!");
        }
        else {
            System.out.println("OMG, look at these beautiful " + getEyecolor() + " eyes!");
        }
    }
}
  • Вопрос задан
  • 115 просмотров
Решения вопроса 1
@12rbah
1 ошибка - конфликтуют имена конструкторов (нельзя создавать 2 конструктора с одинаковой сигнатурой).
2 ошибка - неправильное использование условных операторов.
Ниже исправленный код.
public class People {
    private String eyecolor;
    private String gender;

    
    People(){
        this.setEyecolor("Blue");
    }

        People(String x) {
        this.setEyecolor(x);
    }

    public void setEyecolor(String x) {
        this.eyecolor = x;
    }

    public String getEyecolor() {
        return eyecolor;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String d) {
        this.gender = d;
    }
}

class Myclass {
    public static void main(String [] args) {
        People Mary = new People();
        People Joe = new People("Brown");
        Mary.setGender("female");
        Joe.setGender("male");
        OMG(Joe);
    }
    public static void OMG(People h) {
        if(h.getGender()=="male") {
            System.out.println("OMG, look at his beautiful " + h.getEyecolor() + " eyes!");
        }
        else if(h.getGender()=="female") {
            System.out.println("OMG, look at her beautiful " + h.getEyecolor() + " eyes!");
        }
        else {
            System.out.println("OMG, look at these beautiful " + h.getEyecolor() + " eyes!");
        }
    }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы