Java 如果错误,如何让程序多次询问输入日期
嗨,我正在修复这个程序 编写一个Java程序,显示1900到2099年间任何一年中任何月份的日历 程序必须:Java 如果错误,如何让程序多次询问输入日期,java,loops,calendar,Java,Loops,Calendar,嗨,我正在修复这个程序 编写一个Java程序,显示1900到2099年间任何一年中任何月份的日历 程序必须: 1.用有意义的消息提示用户特定月份 如果输入值为有效日期(即3个字符表示所需月份(即1月、2月,…,小写和大写字母的任意组合,后跟一个空格和一个介于1900和2099之间的整数),则必须显示给定日期的日历 2.1.如果输入值不是有效日期,则必须通知用户该日期不可接受,然后再次提示用户输入另一个日期。如果给出了三个以上的连续错误日期,则必须使用适当的错误消息终止程序 2.2.显示日历后,程
1.用有意义的消息提示用户特定月份
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 int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 28;
} else {
return 29;
}
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 boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
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(i, year);
}
int startday = (days % 7) + 2;
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 scan = null;
Scanner kb = new Scanner(System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
System.out.print(" ");
String month;
int year;
month = kb.next();
year = kb.nextInt();
int yearno = year;
int monthno = getMonthNumber(month);
if (year > 2099)
System.out.println(" Invalid Year!");
if (year < 1900);
System.out.println(" Invalid Year!");
System.out.println();
displayCalendar(monthno, yearno);
System.out.println();
System.out.println();
System.out.println("Do you want to continue? y/n ");
public static int getMonthNumber(字符串s){
如果(s.equalsIgnoreCase(“一月”)){
返回1;
}
如果(s.equalsIgnoreCase(“二月”)){
返回2;
}
如果(s.equalsIgnoreCase(“mar”)){
返回3;
}
如果(s.equalsIgnoreCase(“apr”)){
返回4;
}
如果(s.equalsIgnoreCase(“五月”)){
返回5;
}
如果(s.equalsIgnoreCase(“jun”)){
返回6;
}
如果(s.equalsIgnoreCase(“七月”)){
返回7;
}
如果(s.equalsIgnoreCase(“八月”)){
返回8;
}
如果(s.equalsIgnoreCase(“sep”)){
返回9;
}
如果(s.equalsIgnoreCase(“十月”)){
返回10;
}
如果(s.equalsIgnoreCase(“nov”)){
返回11;
}
如果(s.equalsIgnoreCase(“dec”)){
返回12;
}否则{
System.out.println(“无效月份!”);
}
返回0;
}
公共静态int getDaysIn(int月,int年){
开关(月){
案例1:
返回31;
案例2:
如果(年(月)){
返回28;
}否则{
返回29;
}
案例3:
返回31;
案例4:
返回30;
案例5:
返回31;
案例6:
返回30;
案例7:
返回31;
案例8:
返回31;
案例9:
返回30;
案例10:
返回31;
案例11:
返回30;
案例12:
返回31;
违约:
返回-1;
}
}
公共静态布尔值isLeapYear(整数年){
整月=0;
int s=getDaysIn(月,年);
返回年份%4==0&(年份%100!=0)| |(年份%400==0);
}
公共静态字符串getMonthName(整数月){
开关(月){
案例1:
返回“一月”;
案例2:
返回“二月”;
案例3:
返回“三月”;
案例4:
返回“四月”;
案例5:
返回“可能”;
案例6:
返回“六月”;
案例7:
返回“七月”;
案例8:
返回“八月”;
案例9:
返回“九月”;
案例10:
返回“十月”;
案例11:
返回“十一月”;
案例12:
返回“十二月”;
违约:
返回“无效月份”;
}
}
公共静态整数getStartDay(整数月,整数年){
整数天=0;
对于(int i=1900;i2099)
System.out.println(“无效年份!”);
如果(年份<1900);
System.out.println(“无效年份!”);
System.out.println();
显示日历(monthno、Dearno);
System.out.println();
系统
int monthno = 0;
while (monthno == 0) {
System.out.print("Give the first three letters of a month");
month = kb.next();
monthno = getMonthNumber(month);
}