gpt4 book ai didi

Java:构造函数和传递变量时出错

转载 作者:行者123 更新时间:2023-12-02 04:20:15 25 4
gpt4 key购买 nike

我正在尝试以 dd mm yyyy 格式输入日期,并且必须以 e 格式返回。 G。 9 月 29 日星期二。我是 Java 新手,不确定日、月、年类中的值是否返回到主类中。 Date date1 = new Date(day1,month1,year1) 出现错误,表示它未定义并使其成为构造函数。如果我将其设为构造函数并运行该程序,我会得到类似 lab2.Day@659e0bfd 的值。不知道从这里去哪里。

package lab2;

public class Calendar {

//assume all months have 30 days and that 1 January is a Monday;

public static void main(String[] args) { // e.g. input: 29 09 2015

int in1 = Integer.parseInt(args[0]);
int in2 = Integer.parseInt(args[1]);
int in3 = Integer.parseInt(args[2]);

Day day1 = new Day(in1, in2); // string
Month month1 = new Month(in2); // string
Year year1 = new Year(in3); // integer


Date date1 = new Date(day1, month1, year1); //

date1.printName(); // e.g. "The date is Tuesday, 29th of September, 2015

}
}
<小时/>
package lab2;

public class Day {
private int day;
private int month;
private int code;
private String name; //e.g. Tuesday

public Day(int dy, int mth ){
day = dy;
month = mth;
code = ((day*month) % 7);


// Assigning the day variable number to text //
switch (code) {
case 0: name = "Monday";
break;
case 1: name = "Tuesday";
break;
case 2: name = "Wednesday";
break;
case 3: name = "Thursday";
break;
case 4: name = "Friday";
break;
case 5: name = "Saturday";
break;
case 6: name = "Sunday";
break;
default: System.out.println("Incorrect Day Entered");}

}

}
<小时/>
package lab2;

public class Month {

private int month1;
private String monthName;




public Month(int temp1){
month1=temp1;


switch (month1) {
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "August";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
break;
default: System.out.println("Incorrect Month Error");}


}

}
<小时/>
package lab2;

public class Year {

private int yearNumber;

public Year(int temp2)
{
yearNumber=temp2;


}
}
<小时/>
package lab2;

public class Date {

private String day;
private String month;
private int year;



public Date(String temp1, String temp2, int temp3){
day = temp1;
month = temp2;
year = temp3;
}



public void printName() {
System.out.println("The date is " +day+ ", " +month+ ", " +year);
}

}

最佳答案

There is an error for the "Date date1 = new Date(day1, month1, year1)" saying it is undefined and to make it a constructor.

因为 Date 类中没有像 Date(Day d, Month m, Year y) 这样的参数化构造函数。它有构造函数 Date(String temp1, String temp2, int temp3)。所以参数类型不同。

而不是

Date date1 = new Date(day1, month1, year1);

使用

Date date1 = new Date(day1.name, month1.monthName, year1.yearNumber); 

关于Java:构造函数和传递变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com