我的程序现在做什么:
- 我使用以下信息创建一个人员对象:
firstname
、lastname
、birthdate
(数据是不同的类)。 - 日期类有四个变量:日、月、年和 18+(是或否)。
什么有效:我可以成功创建一个具有名字
、姓氏
和出生日期
的人物对象。
我的人员类(class)(看起来有效)。
public class Person {
public String firstName;
public String lastName;
public Date date;
public String toString() {
return (firstName + " " + lastName + " (" + date);
}
public Person(String firstName, String lastName, Date date) {
this.firstName = firstName;
this.lastName = lastName;
this.date = date;
}
}
我的类(class)包括我的主要类(class),其中我还有一个创建我的人的方法。
public static Person setName() {
String name;
String lastName
String inputBirthdate;
Date niceDate
Date newDate;
System.out.println("Firstname:");
firstName = userInput();
System.out.println("Lastname:");
lastName = userInput();
System.out.println("Birthday:");
inputBirthdate = userInput();
niceDate = new Date(inputBirthdate);
newDate = new Date(niceDate);
return new Gast(firstName, lastName, newDate);
}
然后我有我的 Date 类,我在其中检查日期的输入是否正确。请注意,我可以让我的 Date 类在没有第四个变量的情况下正常工作。
public class Date {
public String day;
public String month;
public String year;
public boolean child;
public String toString() {
return (day + "." + month + "." + year + "." + child);
}
/*Date(String day, String month, String year, boolean child) {
this.day = dag;
this.month = month;
this.year = year;
this.child = child;
}*/ //don't need this one, output is the same
public Date(Datum niceDate) {
int bYear = Integer.parseInt(niceDate.year;
int bMonth = Integer.parseInt(niceDate.day);
int bDay = Integer.parseInt(niceDate.day);
boolean child = false;
if (bYear > 1995) {
this.child= true;
} else if (bYear == 1995 && bMonth > 10) {
this.child = true;
} else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
this.child = true;
} else {
this.child = false;
}
}
public Date(String birthdate) {
String patroon = "\\d{2}-\\d{2}-\\d{4}";
boolean b = birthdate.matches(patroon);
if (b) {
String[] str = birthdate.split("-");
for (String s: str)
this.day = str[0];
this.month = str[1];
this.year = str[2];
}
else {
System.out.println("Birthday is formatted wrong");
}
}
}
如果我运行这个(无论是否检查成人(检查看起来有效!),但是,我输入的 birthdate
返回 null:
Room 1: Name name (null.null.null)false //boolean works, date not
Room 2: available
我认为问题在于,在我的 Date
类中的第二个方法中,public Date(Date Nicedate)
在将我的日期解析为 int 后将其删除。
所以基本上我只想返回 boolean 值并保持我的字符串完全相同,并且只编辑它们以将它们用作 Int 进行计算。
有人能指出我正确的方向吗?也许这是一个非常简单的解决方案,但我已经研究了一整天,但没有看到解决方案。
按要求编辑:(我在公共(public)日期(datum NiceDate)中有此声明,但日期仍然不会显示。嗯嗯:
public Date(Datum niceDate) {
this.year = year;
this.day = day;
this.month = month;
int bYear = Integer.parseInt(niceDate.year;
int bMonth = Integer.parseInt(niceDate.day);
int bDay = Integer.parseInt(niceDate.day);
boolean child = false;
if (bYear > 1995) {
this.child= true;
} else if (bYear == 1995 && bMonth > 10) {
this.child = true;
} else if (bYear == 1995 && bMonth == 10 && bDay > 1) {
this.child = true;
} else {
this.child = false;
}
}
public Date(String birthdate) {
String patroon = "\\d{2}-\\d{2}-\\d{4}";
boolean b = birthdate.matches(patroon);
if (b) {
String[] str = birthdate.split("-");
for (String s: str)
this.day = str[0];
this.month = str[1];
this.year = str[2];
}
else {
System.out.println("Birthday is formatted wrong");
}
}
}
最佳答案
问题是,当您从 niceDate
创建 newDate
时,您并没有复制日/月/年。
如果您查看 public Date(Datum NiceDate)
构造函数,您设置的唯一实例变量是 this.child
,但您还需要设置 this.day
、this.month
和 this.year
。
此外,我建议您创建一个名为 isAdult
的日期计算函数作为 Date
类的方法,然后只需调用 niceDate.isAdult( )
如果您需要显示日期是否是 18 年前。否则,很容易出错,导致 this.child
不正确。
关于java - 我想编辑我的类的第四个变量,但我似乎将它们全部编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19201017/