Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想编辑我的类的第四个变量,但我似乎编辑了所有变量_Java_Class_Oop - Fatal编程技术网

Java 我想编辑我的类的第四个变量,但我似乎编辑了所有变量

Java 我想编辑我的类的第四个变量,但我似乎编辑了所有变量,java,class,oop,Java,Class,Oop,我的程序现在所做的是: 我使用以下信息创建person对象:firstname,lastname,birthdate(数据是不同的类) date类有四个变量:天、月、年和18+(是或否) 工作原理:我可以成功地创建一个名为firstname、lastname和birthdate的person对象 我的person类(看起来很有效) } 我的类,包括我的main,在这里我还有一个创建我的person的方法 然后我有我的Date类,在那里我检查输入的日期是否正确。请不要告诉我,如果没有第四个变量

我的程序现在所做的是:

  • 我使用以下信息创建person对象:
    firstname
    lastname
    birthdate
    (数据是不同的类)
  • date类有四个变量:天、月、年和18+(是或否)
工作原理:我可以成功地创建一个名为
firstname
lastname
birthdate
的person对象

我的person类(看起来很有效)

}

我的类,包括我的main,在这里我还有一个创建我的person的方法

然后我有我的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");
    } 
}
}

但是,如果我运行此操作(无论是否为成人检查(检查似乎有效!),我输入的
生日将返回空值:

   Room 1: Name name (null.null.null)false    //boolean works, date not
   Room 2: available
我认为问题在于,在我的
Date
类的第二个方法中,
publicdate(datenicedate)
将我的日期解析为int后删除

因此,基本上我只想返回布尔值并保持字符串完全相同,并且只想编辑它们以将其用作计算的Int

有人能给我指出正确的方向吗?也许这是一个非常简单的解决方案,但我整天都在努力,没有看到解决方案

按要求编辑:(我已经在公共日期(datum niceDate)中显示了此语句,但该日期仍然不会显示。Hmmmmm:

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
时,您没有复制日/月/年

如果查看
公共日期(Datum niceDate)
构造函数,您设置的唯一实例变量是
this.child
,但您还需要设置
this.day
this.month
this.year

另外,我建议您为日期计算创建一个名为
isadalt
的函数,作为
date
类的一种方法,如果您需要显示日期是否在18年前,只需调用
niceDate.isadalt()
。否则,很容易出错,并且让
这个.child
不正确

public Date(Datum niceDate) {
    this.year = year; 
在此构造函数中,
this.year
year
引用相同的成员变量。因此,您将该变量的值分配给自身

那以后你会的

int bYear = Integer.parseInt(niceDate.year);
它解析来自
niceDate
的值,并将其值分配给名为
bYear
局部变量。相反,您需要将
parseInt
的结果分配给今年的

this.year = Integer.parseInt(niceDate.year);

您可以对所有其他变量进行类似的更改。

您可以共享您的
main
方法吗?
public Date(基准日期)
从不设置
日期
月份
年份
字段。>布尔child=false;它是未设置的if(bYear>1995){this.child=true;}else if(bYear==1995&&bmmonth>10){this.child=true;}else if(bYear==1995&&bmmonth==10&&bDay>1){this.child=true;}else{this.child=false;}这可以简化为一条语句。@代码大师我试过了,但不知怎么的,也没用。您好,谢谢您的快速回复。我在我的公共日期(Date niceDate)中随意添加This.month=month,This.day=day,This.year=year但是,它仍然会输出null。我会查看您的推荐,看看是否可以解决。谢谢:)
int bYear = Integer.parseInt(niceDate.year);
this.year = Integer.parseInt(niceDate.year);