英文:
Trouble with display Date of Birth on Display
问题
我已经创建了程序来尝试读取我的出生日期,但在初始化程序后,它只显示 0。请帮忙,我已经导入了 Java 的 LocalDate、Period 和 Date。
public studentInfo(char gender, String subject1, String subject2) {
this.gender = gender;
this.subject1 = subject1;
this.subject2 = subject2;
}
// 访问方法
public String getName() {
return name;
}
public int getAge() {
LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(1996, 9, 8);
age = Period.between(birthDate, today).getYears();
return age;
}
// 更改方法
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
// 显示信息
public void displayInfo() {
System.out.printf("Name:%s%n", name);
System.out.println("Age:" + age);
System.out.printf("Gender:%c%n", gender);
System.out.printf("Subjects:%s,%s%n", subject1, subject2);
Date date = new Date();
System.out.println(date.toString());
}
}
class NgLengPoh_Lab_1 {
public static void main(String[] args) {
studentInfo getInfo = new studentInfo('M', "CSIT121", "CSIT128");
getInfo.setName("Ng Leng Poh");
getInfo.displayInfo();
}
}
英文:
I've created my program to try to read my date of birth but after initialising my program, it just displays 0. Please help, I have imported java local date, period, and date
<!-- language: lang-java -->
public studentInfo(char gender, String subject1,String subject2)
{
this.gender=gender;
this.subject1=subject1;
this.subject2=subject2;
}
//accessor method
public String getName()
{
return name;
}
public int getAge()
{
LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(1996, 9, 8);
age=Period.between(birthDate, today).getYears();
return age;
}
//mutator method
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age=age;
}
//Display Info
public void displayInfo()
{
System.out.printf("Name:%s%n",name);
System.out.println("Age:"+age);
System.out.printf("Gender:%c%n",gender);
System.out.printf("Subjects:%s,%s%n",subject1,subject2);
Date date = new Date();
System.out.println(date.toString());
}
}
class NgLengPoh_Lab_1
{
public static void main(String [] args)
{
studentInfo getInfo=new studentInfo('M',"CSIT121","CSIT128");
getInfo.setName("Ng Leng Poh");
getInfo.displayInfo();
}//end of main constructor
}//end of class
答案1
得分: 1
The function, getAge()
has to be called where you expect the age.
Replace System.out.println("Age:" + age);
with System.out.println("Age:" + getAge());
By default, an int
variable is initialized with 0
and that is what you are getting as you haven't initialized age
with any value.
英文:
The function, getAge()
has to be called where you expect the age.
Replace System.out.println("Age:"+age);
with System.out.println("Age:" + getAge());
By default, an int
variable is initialized with 0
and that is what you are getting as you haven't initialized age
with any value.
答案2
得分: 1
这是因为程序中没有调用年龄的getter方法,而getter方法包含计算年龄的逻辑。因此,它返回int的默认值,即0。
在displayInfo()内部,尝试调用年龄的getter方法,而不是直接访问该变量。
public void displayInfo()
{
System.out.printf("Name:%s%n", name);
System.out.println("Age:" + getAge());
System.out.printf("Gender:%c%n", gender);
System.out.printf("Subjects:%s,%s%n", subject1, subject2);
Date date = new Date();
System.out.println(date.toString());
}
英文:
This is so because, the getter method for age is not being called anywhere in the program and the getter method consists the logic to calculate the age. Hence, it is returning the default value of int which is 0.
Inside displayInfo() try calling the getter method of age, instead of directly accessing the variable.
public void displayInfo()
{
System.out.printf("Name:%s%n",name);
System.out.println("Age:"+getAge());
System.out.printf("Gender:%c%n",gender);
System.out.printf("Subjects:%s,%s%n",subject1,subject2);
Date date = new Date();
System.out.println(date.toString());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论