我正在使用切换案例语句,我必须在某些给定输入之后以年/月/日的格式打印星期几。这是到目前为止完成的类,以及我对printDayOfWeek()
类中的Date
方法所做的事情:
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
public int getDayOfWeek() {
int y0 = year - (14 - month) / 12;
int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
int m0 = month + 12 * ((14 - month) / 12) - 2;
int d0 = (day + x + (31 * m0) / 12) % 7;
return d0;
}
public int getDaysInMonth(int month) {
int daysInMonth = (int) (28 + (Math.floor(month / 8.0) + month) % 2 + 2 % month + 2 * Math.floor(1.0 / month));
if (month == 2 && isLeapYear()) {
daysInMonth += 1;
}
return daysInMonth;
}
public boolean isLeapYear() {
boolean isLeapYear = true;
if (year % 4 != 0) {
isLeapYear = false;
}
else {
if (year % 100 != 0) {
isLeapYear = true;
}
else if (year % 400 != 0) {
isLeapYear = false;
}
else {
isLeapYear = true;
}
}
// Task I.1
return isLeapYear;
}
public void printDaysInMonth() {
// Task I.2
for (int i = 1; i <= 12; i++) {
int m = getDaysInMonth(i);
System.out.println( i + " " + m);
}
}
public void printDaysInYear() {
// Task II
}
public void printDayOfWeek() {
// Task III
int d0 = getDayOfWeek();
switch(d0) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
case 4:
System.out.println("Thursday");
case 5:
System.out.println("Friday");
case 6:
System.out.println("Saturday");
case 7:
System.out.println("Sunday");
}
}
}
我的Prinddayofweek方法有什么问题? 既然真的不知道要切换案件,那时候真的不知道该怎么办。
答案 0 :(得分:0)
这是您的printDayOfWeek()函数的样子:-在执行%7时,星期日将具有值0。在每个系统结束时停止执行流程。
public void printDayOfWeek() {
// Task III
int d0 = getDayOfWeek();
switch(d0) {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
}
}