获取定义的值并在字符串方法Java中使用它们

时间:2015-02-02 22:05:48

标签: java string methods setter getter

大家好,我遇到了一个我正在尝试创建的方法的问题。我的方法应该采用先前在课程中定义的月,日和年,然后用" /"分数。因此,如果该顺序中的月,日,年的值为1,2,14,则打印为1/2/14(我使用单独的类来测试它是否有效)。到目前为止,我的方法是:

public String DateTest(){
    DateTest = month + day + year;
    return DateTest;
}

with public String DateTest;用其余的值定义在它上面。如何创建一个返回三个整数值的方法&#34; /&#34; s除以它们(我不确定如何获得&#34; /&#34; s)?< / p>

以下是我将初始化变量的代码:

public class Date {
public int month;
public int day;
public int year;
public String DateTest;

public void setMonth(int month){
    this.month = month;
}
public int getMonth(){
    return month;
}

public void setDay(int day){
    this.day = day;
}
public int getDay(){
    return day;
}

public void setYear(int year){
    this.year = year;
}
public int getYear(){
    return year;
}

1 个答案:

答案 0 :(得分:1)

只需通过以String的形式连接所需的字符来创建所需的String,然后返回值:

public String yourDateTestWithSlashes() {
    return month + "/" + day + "/" + year;
               //^-----^ <-- this does the "magic"
}