gpt4 book ai didi

java - 输入年份和第一天,获取一个月的第一天

转载 作者:行者123 更新时间:2023-12-01 07:56:43 25 4
gpt4 key购买 nike

这是到目前为止我的代码。我想我的做法都是错误的。帮助将不胜感激:

输入年份为 2013 年;天的输入为 2(表示星期二)

public class firstMonthDay {

public static void main(String[] args) {

Scanner input= new Scanner(System.in);

System.out.print("Enter Year:");
int year = input.nextInt();

System.out.print("Enter day of the week:");
int inputDay = input.nextInt();

int firstday=0;
int daysInMonth=0;
int month =0;

for (int i=1; i<=365; i++){

switch(daysInMonth){

case 1: daysInMonth += 31 ;
case 2: daysInMonth += 28 ;
case 3: daysInMonth += 31 ;
case 4: daysInMonth += 30 ;
case 5: daysInMonth += 31 ;
case 6: daysInMonth += 30 ;
case 7: daysInMonth += 31 ;
case 8: daysInMonth += 31 ;
case 9: daysInMonth += 30 ;
case 10: daysInMonth += 31 ;
case 11: daysInMonth += 30 ;
case 12: daysInMonth += 31 ;
break;
default:


switch( firstday=daysInMonth-inputDay%7){
case 1:System.out.print("Monday");break;
case 2:System.out.print("Tuesday");break;
case 3:System.out.print("Wednesday");break;
case 4:System.out.print("Thursday");break;
case 5:System.out.print("Friday");break;
case 6:System.out.print("Saturday");break;
case 7:System.out.print("Sunday");break;
default:

while (month<12){
month++;
switch(month){
case 1: System.out.println("January");break;
case 2: System.out.println("Febuary");break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April");break;
case 5: System.out.println("May");break;
case 6: System.out.println("June");break;
case 7: System.out.println("july");break;
case 8: System.out.println("August");break;
case 9: System.out.println("September");break;
case 10: System.out.println("October");break;
case 11: System.out.println("November");break;
case 12: System.out.println("December");break;
default:

}
}
}
}
System.out.println("the first day of"+month+"is:"+firstday);
}
}
}

结果应该是

"the first day of january is tuesday"
......
"the first day of december is Sunday"

最佳答案

日历是你的 friend 。

Scanner input= new Scanner(System.in);
System.out.print("Enter Year:");
int year = input.nextInt();
Calendar cal = Calendar.getInstance();
for (int i=0; i<12; i++) {
cal.set(year, i, 1);
System.out.printf( "the first day of %s is %s\n",
cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()),
cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()) );
}//end for i

关于java - 输入年份和第一天,获取一个月的第一天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206598/

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