【问题标题】:How to make a program ask for a input date multiple times if wrong如果错误,如何使程序多次要求输入日期
【发布时间】:2016-02-26 01:44:13
【问题描述】:

你好,我正在尝试修复这个程序,

编写一个 Java 程序,显示 1900 年到 2099 年之间任何一年的任何月份的日历。

程序必须:
1. 用有意义的消息提示用户特定月份。

  1. 如果输入值是有效日期(即 3 个字符指定所需月份(即 JAN、FEB、...,大小写字母的任意组合,后跟空格和 1900 到 2099 之间的整数) ),则必须显示给定日期的日历

2.1。如果输入值不是有效日期,则必须通知用户该日期不可接受,然后再次提示输入另一个日期。如果给出的连续错误日期超过三个,则必须终止程序并显示相应的错误消息。

2.2。显示日历后,程序必须询问用户是否想输入另一个日期(你想继续吗?)

2.3。用户必须以小写和大写字母的任意组合回答是、否、y 或 n

2.4。如果给出的答案无效,则必须通知用户该答案不可接受,然后再次提示用户提供另一个答案。如果给出的连续错误答案超过三个,则程序必须以适当的错误消息终止

2.5。如果步骤 2.2 中的答案为“是”(以任何允许的形式),则程序必须从步骤 1 继续。如果答案为“否”(以任何允许的形式),则程序必须终止,并显示一条消息,通知程序正常终止。

  1. 所有输入都必须使用 Scanner 对象完成,即 Scanner kb = new Scanner (System.in);

  2. 所有输出都应使用 System.out.print 或 System.out.println

到目前为止,我已经达到了 2.1,我在程序中的 if 语句告诉用户日期错误但是我正在努力如何向用户询问另一个日期,以及如果用户继续输入不正确的日期这 3 次才终止程序,如果用户输入正确的日期,则继续打印日历。

这是我目前所拥有的

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 ");

如果年份错误,它会打印无效年份,但现在我坚持如何在结束程序之前再次询问 3 条领带/如果用户在 3 次尝试之一中输入正确的日期,则继续。到目前为止,使用我的程序,如果用户输入有效日期,则会打印正确的日历

【问题讨论】:

    标签: java loops calendar


    【解决方案1】:

    你需要做的是循环

      int monthno = 0;
      while (monthno == 0) {
         System.out.print("Give the first three letters of a month");
         month = kb.next();
         monthno = getMonthNumber(month);          
     }
    

    对其他输入做同样的事情

    顺便说一句,如果您在ArrayList 中继续游览months,然后简单地执行get 来获得价值,那么您的代码将会非常干净。它要么存在,要么不存在。

    【讨论】:

      猜你喜欢
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多