简体   繁体   English

如何为这个特定变量设置 N/A?

[英]How to set N/A for this particular variable?

I'd like to set N/A for the variable experience if the value is 0 for the employee experience.如果员工体验的值为 0,我想为变量体验设置 N/A。

So if the users inputs 0 number of experience for a job, the program should return N/A.因此,如果用户输入 0 个工作经验,程序应返回 N/A。 How do I do that in this given class?在给定的 class 中,我该怎么做?

public class EmployeeService () {
    private String experience;
    private String position;
    
    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }
    
    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }
    
    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
    
    public String toString() {
        return "Experience - " + experience + "Position" + " - " + position;
    }
    
}

Just do it in the setter as follows:只需在 setter 中执行以下操作:

public void setExperience(String experience) {
    if ("0".equals(experience)) {
        this.experience = "N/A";
    } else {
        this.experience = experience;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM