gpt4 book ai didi

java - 错误的星期几

转载 作者:行者123 更新时间:2023-11-29 07:47:52 25 4
gpt4 key购买 nike

我正在尝试用 java 编写一个简单的程序用户将输入他的生日然后应用程序给他日期这是我的代码

import java.util.*;

public class DOW {
public static void main(String [] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter Your Birthdate");
System.out.print("Day : ");
int day = scn.nextInt();
System.out.print("Month : ");
int month = scn.nextInt();
System.out.print("Year : ");
int year = scn.nextInt();
scn.close();

Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
int dow = cal.get(Calendar.DAY_OF_WEEK);
switch (dow){
case 1:
System.out.println("Your Birthdate : " + "Sunday - " + day + "/" + month + "/" + year);
break;
case 2:
System.out.println("Your Birthdate : " + "Monday - " + day + "/" + month + "/" + year);
break;
case 3:
System.out.println("Your Birthdate : " + "Tuesday - " + day + "/" + month + "/" + year);
break;
case 4:
System.out.println("Your Birthdate : " + "Wednesday - " + day + "/" + month + "/" + year);
break;
case 5:
System.out.println("Your Birthdate : " + "Thursday - " + day + "/" + month + "/" + year);
break;
case 6:
System.out.println("Your Birthdate : " + "Friday - " + day + "/" + month + "/" + year);
break;
case 7:
System.out.println("Your Birthdate : " + "Saturday - " + day + "/" + month + "/" + year);
break;
}
}
}

但是日期名称不正确例如我的生日是 18/1/1987 它是星期日我的代码的结果是星期四

注意int month2 = month1-1;我写这个是因为 java 中的月份以 0 而不是 1 开头

所以有帮助吗??

最佳答案

因为Calender.set需要参数的顺序:

public final void set(int year, int month, int date);

而且你的顺序有误。将其更改为:

cal.set(year, month2, day);

你会得到正确的结果。

关于java - 错误的星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23958259/

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