英文:
Find day from String array based on iteration using Java
问题
我有一个字符串数组和以下输入
S = "Tue",K = 23
String[] daysOfWeek = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
我将我的输入设置为 "Tue",以查找在 10 天后的下一个日期。我期望的答案是 "Fri"
以下是我的代码
String[] daysOfWeek = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
int day = Arrays.asList(daysOfWeek).indexOf(S);
int n = K % 7;
if ((day + n) > 6) {
int index = (day + n) % 6;
//return daysOfWeek[ ];
} else {
return daysOfWeek[day + n];
}
return null;
有什么想法吗
英文:
I have my string array and the input as follows
S = "Tue" and K = 23
String[] daysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
I give my input as "Tue" and to find the next day after 10 days. I'm expecting answer as "Fri"
Here is my code
String[] daysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int day = Arrays.asList(daysOfWeek).indexOf(S);
int n = K % 7;
if((day + n) > 6) {
int index = (day + n) % 6;
//return daysOfWeek[ ];
} else {
return daysOfWeek[day + n ];
}
return null;
Any ideas
答案1
得分: 4
可以使用 java.time.DayOfWeek
,而不是重新发明轮子。
例如:
DayOfWeek dayOfWeek = DayOfWeek.TUESDAY;
DayOfWeek dayOfWeek1 = dayOfWeek.plus(10);
System.out.println(dayOfWeek1); // FRIDAY
英文:
You can use java.time.DayOfWeek
instead of reinventing the wheel.
For example:
DayOfWeek dayOfWeek = DayOfWeek.TUESDAY;
DayOfWeek dayOfWeek1 = dayOfWeek.plus(10);
System.out.println(dayOfWeek1); // FRIDAY
答案2
得分: 1
private static final String[] DAYS_OF_WEEK = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
private static final Map<String, Integer> MAP = Map.of(DAYS_OF_WEEK[0], 0, DAYS_OF_WEEK[1], 1,
DAYS_OF_WEEK[2], 2, DAYS_OF_WEEK[3], 3, DAYS_OF_WEEK[4], 4, DAYS_OF_WEEK[5], 5, DAYS_OF_WEEK[6], 6);
public static String find(String dayOfWeek, int days) {
return DAYS_OF_WEEK[(MAP.get(dayOfWeek) + days) % 7]; // NPE if not found
}
英文:
Be simple!
private static final String[] DAYS_OF_WEEK = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
private static final Map<String, Integer> MAP = Map.of(DAYS_OF_WEEK[0], 0, DAYS_OF_WEEK[1], 1,
DAYS_OF_WEEK[2], 2, DAYS_OF_WEEK[3], 3, DAYS_OF_WEEK[4], 4, DAYS_OF_WEEK[5], 5, DAYS_OF_WEEK[6], 6);
public static String find(String dayOfWeek, int days) {
return DAYS_OF_WEEK[(MAP.get(dayOfWeek) + days) % 7]; // NPE if not found
}
答案3
得分: 0
你可以对天数取模7,然后根据输出找到从起始位置移动到结果的索引。
英文:
You can do modulo 7 for the number of days after, and then you can find based on the output move the index to the result from the starting position.
答案4
得分: 0
混合方法 -
String[] daysOfWeek = {"周一","周二","周三","周四","周五","周六","周日"};
int today = LocalDate.now().getDayOfWeek().getValue()-1;
for (int i = 0; i < 31; i++) {
System.out.println(daysOfWeek[today] + " >>> " + i + " - " + daysOfWeek[(today+i)%7]);
}
英文:
a mixed approach -
String[] daysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int today = LocalDate.now().getDayOfWeek().getValue()-1;
for (int i = 0; i < 31; i++) {
System.out.println(daysOfWeek[today] + " >>> " + i + " - " + daysOfWeek[(today+i)%7]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论