英文:
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("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();
}
}
答案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("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();
}
}
Output :
Date of Birth:08 Sep,1996
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论