我刚开始学习在我的教育中使用Java编程。这意味着,我还是Java的新手。
我有一些代码,我应该制作一个名为birthday()的方法,让员工年满一岁,然后展示员工的新年龄。
我有一个名为Employee.java的类和一个名为EmployeeApp.java的类。
Employee.java代码如下所示:
package model;
public class Employee {
private String name;
private boolean trainee;
private int age;
public Employee(String inputName, int inputAge) {
name = inputName;
age = inputAge;
trainee = true;
}
public void setName(String inputName) {
name = inputName;
}
public void setAge(int inputAge) {
age = inputAge;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int birthday() {
agePlusOne = age + 1;
}
public void setTrainee(boolean isTrainee) {
trainee = isTrainee;
}
public boolean isTrainee() {
return trainee;
}
public void printEmployee() {
System.out.println("*******************");
System.out.println("Name: " + name);
System.out.println("Trainee: " + trainee);
System.out.println("Age: " + age);
System.out.println();
}
}
EmployeeApp.java代码如下所示:
package model;
public class EmployeeApp {
public static void main(String[] args) {
Employee e1 = new Employee("Hans Jensen", 23);
e1.printEmployee();
Employee e2 = new Employee("Rasmus Nielsen", 32);
e2.printEmployee();
}
}
任何帮助将不胜感激! : - )
答案 0 :(得分:1)
public int birthday() {
return age++;
}
此后在printEmployee()方法中添加一行代码System.out.println("Age+1: is"+birthday());
)
完整代码:
public void printEmployee() {
System.out.println("*******************");
System.out.println("Name: " + name);
System.out.println("Trainee: " + trainee);
System.out.println("Age: " + age);
System.out.println("Age+1: is"+birthday());//add this line
System.out.println();
}
答案 1 :(得分:1)
首先你应该问问自己,你真正被问到了什么。我理解它的方式,birthday
方法应该使员工年龄提前一年,因此它应该更改其age
变量的值。
您的birthday
函数将age + 1
存储在新变量agePlusOne
中(未声明且确实应该存在),因此它会进行计算,但不会对员工产生影响#39; s age
。您需要将age + 1
的结果存储在age
中,或者使用现有函数setAge
为您执行此操作(将计算作为参数传递)。
接下来,您需要显示员工的新年龄。我不清楚你应该在哪里这样做。有意义的是拥有一个birthday
函数,该函数在调用时更改年龄,并且不会输出到屏幕(除了可能暂时用于调试目的)。您可以在main
函数中显示其效果,例如在现有函数之后添加两行代码:
// existing lines
Employee e2 = new Employee("Rasmus Nielsen", 32);
e2.printEmployee(); // shows age before calling birthday
// new lines
e2.birthday(); // changes value of age
e2.printEmployee(); // shows age after calling birthday
如果您认为必须在birthday
函数中执行输出,只需复制printEmployee
中显示年龄的行。
答案 2 :(得分:0)
func slideOutMenus() {
if revealViewController() != nil {
hamburgMenuBtn.target = revealViewController()
hamburgMenuBtn.action = #selector(SWRevealViewController.revealToggle(_:))
revealViewController().rearViewRevealWidth = 250
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
但是你不应该这样做,因为打印应该在public int ageUp() {
System.out.println(++age);
return age;
}
方法之外。
答案 3 :(得分:0)
这听起来有点模糊,但您可以将实施更改为age
字段,而不是使用birthday
字段...
public class Employee {
private String name;
private boolean trainee;
private Date birthday; //changes takes place in here
...
}
每当您询问员工年龄时,您都会提到某个特定日期
public int getAgeInYears(Date referenceDate){
int age = ... // some Math to calculate age - please do on your own
return age;
}
在行动中看起来像
int ageOnEntrance = myEmployee.getAgeInYears(new Date(1997));
int ageNow = myEmployee.getAgeInYears(new Date(now()));
int ageOnRetirement = myEmployee.getAgeInYears(new Date(2018)); //lucky guy
int candlesOnCakeIn2002 = myEmployee.getAgeInYears(new Date(2002));
int candlesOnCakeLastYear = myEmployee.getAgeInYears(new Date(2017));
答案 4 :(得分:-1)
更改方法birthday()
:
public int birthday() {
return ++age; //return new age
}