在java中的日历程序中,我尝试编写此代码,但不起作用
我必须用java写一个日历,这些是我的指南。。。 编写一个Java程序,显示1900年和1900年之间任何年份的任何月份的日历 和2099。 每个输出日历必须以以下形式显示:在java中的日历程序中,我尝试编写此代码,但不起作用,java,calendar,Java,Calendar,我必须用java写一个日历,这些是我的指南。。。 编写一个Java程序,显示1900年和1900年之间任何年份的任何月份的日历 和2099。 每个输出日历必须以以下形式显示: January 1987 Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
January 1987
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
如果输入值不是有效日期,则必须通知用户该日期不可接受,然后再次提示用户输入其他日期。如果给出了三个以上的连续错误日期,则必须使用适当的错误消息终止程序
显示日历后,程序必须询问用户是否要输入另一个日期是否要继续
用户必须以大写字母和小写字母的任意组合回答是、否、y或n
这是我到目前为止所拥有的
package assignment.pkg2;
import java.util.Scanner;
public class Assignment2 {
public static int getMonthNumber(String s) {
if (s.equalsIgnoreCase("jan")) {
return 1;
}
if (s.equalsIgnoreCase("feb")) {
return 2;
}
if (s.equalsIgnoreCase("mar")) {
return 3;
}
if (s.equalsIgnoreCase("apr")) {
return 4;
}
if (s.equalsIgnoreCase("may")) {
return 5;
}
if (s.equalsIgnoreCase("jun")) {
return 6;
}
if (s.equalsIgnoreCase("jul")) {
return 7;
}
if (s.equalsIgnoreCase("aug")) {
return 8;
}
if (s.equalsIgnoreCase("sep")) {
return 9;
}
if (s.equalsIgnoreCase("oct")) {
return 10;
}
if (s.equalsIgnoreCase("nov")) {
return 11;
}
if (s.equalsIgnoreCase("dec")) {
return 12;
} else {
System.out.println("Not valid month!");
}
return 0;
}
public static boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 29;
} else {
return 28;
}
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
return -1;
}
}
public static String getMonthName(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month.";
}
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days = days + 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(month, year);
}
int startday = (days + 1) % 7 + 1;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i <= startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
public static void main(String[] args) {
Scanner c = new Scanner(System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
String month, year;
month = c.next();
year = c.next();
int yearno = Integer.parseInt(year);
int monthno = getMonthNumber(month);
displayCalendar(monthno, yearno);
}
}
你的家庭作业的13项要求中哪项不起作用?没有发生的事情不是对行为的有用描述,也不起作用。它会崩溃吗?你必须终止这个程序吗?卡在哪里了?它是否只会因为您发布的所有代码而崩溃,或者您是否可以删除大量代码stackoverflow.com/help/mcve?帮助我们帮助你。当你调用程序时,你会给它什么输入?我刚刚给了它2015年10月,它打印了一个日历,但日期与我桌面日历上的日期不一致。请给出一个月的前三个字母,然后输入年份:1992年8月太阳-周一-周二-周三-周四-周六12 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31建立成功的总时间:14秒没有问题,如果你开始学习一些东西,你不应该试图用一大步征服世界。如果您一步一步地进行并陷入困境,那么您确切地知道代码中哪些是有效的,哪些是无效的,因此您可以提出更精确的问题,并获得更好/精确的答案,或者根本没有答案。