gpt4 book ai didi

java - 根据输入日期检索星期几

转载 作者:行者123 更新时间:2023-12-01 16:48:46 24 4
gpt4 key购买 nike

any1 能告诉我我的代码有什么问题吗?我得到了不正确的输出。干杯

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] days={"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
int month = in.nextInt();
int day = in.nextInt();
int year = in.nextInt();
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
int day_of_week = c.get(Calendar.DAY_OF_WEEK)-1;
System.out.println(days[day_of_week]);
}

最佳答案

阅读文档,然后更改为

c.set(Calendar.MONTH,  month -1);

因为它接受从零开始的索引

关于java - 根据输入日期检索星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44879645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com