我正在使用switch case语句,我必须以年/月/日的格式在一些给定输入之后打印一周中的一天。这是迄今为止完成的类以及我为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");
}
}
}
我的Print Dayofweek方法出了什么问题?因为我刚接触案件,所以不知道该做什么。
这就是你的printDayOfWeek()函数应该是这样的: - 正如你所做的那样%7星期日的值为0.除了那个断点;在每个sys结束时停止执行流程。
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;
}
}