更好的显示Java

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

Better way to display Java

问题

以下是翻译好的部分:

class studentInfo {
    
    private String format;
    
    public String date() {
        LocalDate dob = LocalDate.of(1996, 9, 8);
    	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM, yyyy", Locale.ENGLISH);
    	format = dob.format(formatter);
    	return format;
    }
    
    public void displayInfo() {
        System.out.println("出生日期:" + format);
    }
}
英文:

this is my 2nd class, the display it gives me is "null", is there a solution to my problem? How am I able to display my date of birth by calling the method?

class studentInfo {
    
    private String format;
    
    public String date() {
        LocalDate dob=LocalDate.of(1996, 9, 8);
    	DateTimeFormatter formatter=DateTimeFormatter.ofPattern("8 September,1996");
    	format=dob.format(formatter);
    	return format;
    }
    
    public void displayInfo() {
        System.out.println("Date of Birth:"+format);
    }
}

</details>


# 答案1
**得分**: 2

以下是翻译好的内容

你需要首先调用date()方法因为该方法正在设置实例变量format的值所以当你调用displayInfo()方法时你可以看到设置的值

还更新了Pattern

以下是修改后的代码示例

```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

class StudentInfo {
    private String format;

    public String date() {
        LocalDate dob = LocalDate.of(1996, 9, 8);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        format = dob.format(formatter);
        return format;
    }

    public void displayInfo() {
        System.out.println("Date of Birth:" + format);
    }
}

public class Test {
    public static void main(String[] args) {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.date();
        studentInfo.displayInfo();
    }
}
英文:

You need to call date() method first as this method is setting the value of the instance variable format, so when you call displayInfo(), you could see the set value.

Also updated the Pattern.

Here is the modified code:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class StudentInfo {
private String format;
public String date() {
LocalDate dob = LocalDate.of(1996, 9, 8);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&quot;);
format = dob.format(formatter);
return format;
}
public void displayInfo() {
System.out.println(&quot;Date of Birth:&quot; + format);
}
}
public class Test {
public static void main(String[] args) {
StudentInfo studentInfo = new StudentInfo();
studentInfo.date();
studentInfo.displayInfo();
}
}

答案2

得分: 1

你没有调用date()方法。

更新后的代码:

class studentInfo {

    public String date() {
        LocalDate dob = LocalDate.of(1996, 9, 8);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM,yyyy", Locale.US);
        String format = dob.format(formatter);
        return format;
    }

    public void displayInfo() {
        System.out.println("Date of Birth:" + date());
    }

}

public class Sample {

    public static void main(String[] args) {
        studentInfo info = new studentInfo();
        info.displayInfo();
    }

}

输出:

Date of Birth:08 Sep,1996
英文:

You didn't call the method date().

Updated code :

class studentInfo {
public String date() {
LocalDate dob = LocalDate.of(1996, 9, 8);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd MMM,YYYY&quot;, Locale.US);
String format = dob.format(formatter);
return format;
}
public void displayInfo() {
System.out.println(&quot;Date of Birth:&quot; + date());
}
}
public class Sample {
public static void main(String[] args) {
studentInfo info = new StudentInfo();
info.displayInfo();
}
}

Output :

Date of Birth:08 Sep,1996

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

发表评论

匿名网友

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

确定