所以我正在编写一个名为getThanksgiving
. 它按原样工作,并且是更大课程的一部分,但我需要有关如何提高效率的建议。该getWeekDay
方法只返回用户输入的年份 11 月 1 日是星期几。
public String getThanksgiving(){
String a = getWeekDay(11, 1);
int offset = 0;
if(a.equals("Friday")){
offset = 7;
}
if(a.equals("Saturday")){
offset = 6;
}
if(a.equals("Sunday")){
offset = 5;
}
if(a.equals("Monday")){
offset = 4;
}
if(a.equals("Tuesday")){
offset = 3;
}
if(a.equals("Wednesday")){
offset = 2;
}
if(a.equals("Thursday")){
offset = 1;
}
int date = 21 + offset;
thanksgiving = "Thursday, November " + date;
return thanksgiving;
}
我尝试将其重写为 for 循环,但它不起作用。
public String getThanksgiving(){
String a = getWeekDay(11, 1);
int offset = 8;
String[] wTable = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
for(int i = 1; i < 8; i++){
if(a.equals(wTable[i - 1])){
offset --;
}
}
}
此外,偏移和添加 21 的想法只是我的老师希望我们做的事情。提前致谢!