出现显示出生日期的问题。

huangapple go评论70阅读模式
英文:

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(&quot;Name:%s%n&quot;,name);
System.out.println(&quot;Age:&quot;+age);
System.out.printf(&quot;Gender:%c%n&quot;,gender);
System.out.printf(&quot;Subjects:%s,%s%n&quot;,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(&#39;M&#39;,&quot;CSIT121&quot;,&quot;CSIT128&quot;);
getInfo.setName(&quot;Ng Leng Poh&quot;);
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(&quot;Age:&quot;+age); with System.out.println(&quot;Age:&quot; + 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(&quot;Name:%s%n&quot;,name);
System.out.println(&quot;Age:&quot;+getAge());
System.out.printf(&quot;Gender:%c%n&quot;,gender);
System.out.printf(&quot;Subjects:%s,%s%n&quot;,subject1,subject2);
Date date = new Date();
System.out.println(date.toString());
}

huangapple
  • 本文由 发表于 2020年10月4日 01:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定