我正在尝试以dd mm yyyy
格式输入日期,并且必须以e格式返回。 G。 Tuesday, 29th September
。我是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);
}
}
答案 0 :(得分:1)
“date date1 = new Date(day1,month1,year1)”出错 说它是未定义的并使它成为构造函数。
由于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);
答案 1 :(得分:1)
“date date1 = new Date(day1,month1, year1)“说它是未定义的,并使它成为一个构造函数。如果我做 它是一个构造函数并运行程序我得到的值就像 “lab2.Day@659e0bfd”。不知道从哪里开始。
你得到lab2.Day@659e0bfd因为你没有实现toString()方法。在Day,Month和Year类中实现toString()方法,您将获得所需的结果。检查下面的代码 -
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");}
}
@Override
public String toString() {
return name;
}
}
类似地,您可以将toString()方法放在Month类
中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");
@Override
public String toString() {
return monthName;
}
}
你的年级 -
public class Year {
private int yearNumber;
public Year(int temp2)
{
yearNumber=temp2;
}
@Override
public String toString() {
return yearNumber;
}
}