java - 形成此 if 语句的更好方法?

标签 java if-statement system.out

我目前正在看书学习java,一个项目是在输入月份后输出月份的日期和月份名称。我想知道是否有比我已经完成的更好的方法来设置我的 if 语句。

PS:console reader 只是一个包含的类,可以轻松地从用户控制台获取输入。

public class Project13 {

public static void main(String[] args) {
    ConsoleReader console = new ConsoleReader(System.in);

    System.out.println("Enter a month you would like to evaluate (by number):");
    int month = console.readInt();

    int days = 0;
    String monthout = "Month";
    String out = "Yes";
    if(month == 1){
        days = 31;
        monthout = "January";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 2){
        System.out.println("Is it a leap year? Yes or No:");
        String leap = console.readLine(); 
        if(leap.equalsIgnoreCase("yes")){
            days = 29;
            monthout = "February";
            out = "There are " + days + " days in " + monthout;
        }else if(leap.equalsIgnoreCase("no")){
            days = 28;
            monthout = "February";
            out = "There are " + days + " days in " + monthout;
        }else{
            out = "Something went wrong, please try again";
        }
    }else if(month == 3){
        days = 31;
        monthout = "March";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 4){
        days = 30;
        monthout= "April";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 5){
        days = 31;
        monthout = "May";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 6){
        days = 30;
        monthout = "June";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 7){
        days = 31;
        monthout = "July";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 8){
        days = 31;
        monthout = "August";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 9){
        days = 30;
        monthout = "September";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 10){
        days = 31;
        monthout = "October";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 11){
        days = 30;
        monthout = "November";
        out = "There are " + days + " days in " + monthout;
    }else if(month == 12){
        days = 31;
        monthout = "December";
        out = "There are " + days + " days in " + monthout;
    }else if(month > 12){
        out = "Your month input was not valid. Please try again.";
    }

    System.out.println(out);
}

}

最佳答案

您几乎可以用一对数组替换整个 if 语句:

int dayCount[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String monthName[] = new String[] {"January", "February", ...};

有了这两个数组,您可以这样做:

// February is the only month that needs special handling
if (month == 2) {
    // Do your special handling of leap year etc...
} else if (month >= 1 && month <= 12) {
    // All other valid months go here. Since Java arrays are zero-based,
    // we subtract 1 from the month number
    days = dayCount[month-1];
    monthout = monthName[month-1];
} else {
    // Put handling of invalid month here
}
out = "There are " + days + " days in " + monthout;

关于java - 形成此 if 语句的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22206073/

相关文章:

java - 重写 Enum 中的 equals、toString、hashcode 方法

java - 历史记录队列不释放第一个文件

在 C 中比较字符串并打印存储的字符串

java - 如何使 Redirect.INHERIT 和 System.setOut 协同工作

java - 以 3 位小数显示秒。 - java

java - 二进制搜索字符串数组

java - 4 键值HashMap?大批?最好的方法?

if-statement - `if` 的替代实现 - 难以理解的行为

ruby - Ruby 中冲突的 if 语句

java - 不带 'ln' 的字符串的 System.out.print