2014-03-05 61 views
1

我目前正在從一本書中學習java,一個項目在輸入月份數後輸出一個月的日期和月份名稱。我想知道是否有更好的方式來設置我的if語句,而不是我已經做了什麼。更好的方式形成這個if語句?

PS:控制檯閱讀器只是一個包含的類,可以輕鬆地從用戶控制檯獲取輸入。

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

} 
+0

不一定好,但你可以嘗試一個'開關case'聲明:http://docs.oracle.com /javase/tutorial/java/nutsandbolts/switch.html – Bucket

回答

9

你可以用這樣的一對陣列的替換幾乎整個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; 
+2

+1。這是唯一的答案,它不僅僅用一系列程序性開關語句來代替一系列程序化的if語句,而是使用合理的編程原則。 –

5
switch(month) { 
    case 1: 
     // stuff 
     break; 
    case 2: 
     // etc..... 

如果你要做到這一點,你一定要使用enum,而不是代表只是個整數...

0

您可以switch-case statements.Avoid這麼多,如果去-else

switch(month) { 
    case 1: 
     days = 31; 
     monthout = "January"; 
     out = "There are " + days + " days in " + monthout; 
     break; 
    case 2: 
     // Add stuff 
     break; 
    default: 
     break; 
} 
0

SWTICH:

switch (month) 
{ 
    case 1: 
     // ... stuff here 
     break; 
    case 2: 
     // ... stuff here 
     break; 
    // ... more cases 
    default: // no case above matched what the month was 
     // .... handle default case 
}; 
0

使用switch語句而不是許多其他-if。它很容易讀取,修改,並與休息的工作速度更快,然後其他人,如果

case 1: 
//code 
break; 
0

我認爲你應該使用一個Map來避開(或交換機)的情況下這一切。你的代碼的主要問題是你的代碼很多。

private static Map<Integer, Integer> monthDays = new HashMap<>(); 

static { 
    monthDays.put(1, 31); 
    monthDays.put(2, 28); 
    ... 
} 

private static int getMonthDays(int month) { 
    if (month == 2) { 
     // handle special case with Februar 
    } 
    return monthDays.get(month); 
} 

public static void main(String[] args) { 
    ...  
    if (month >= 1 && month <= 12) { 
     monthout = getMonthDays(month); 
     ... 
     out = "There are " + days + " days in " + monthout; 
    } else { 
     out = "Your month input was not valid. Please try again."; 
    } 
    System.out.println(out); 
} 
0

開關櫃並不好。如果有超過3-4個if-else或switch條件,可以考慮採用其他方式來執行命令或策略模式等。

例如,創建一個動作地圖,它可以在條件條件下設置並執行所需的操作。示例代碼如下。這僅僅是示例代碼讓我知道如果你需要的任何部分,進一步闡述:

public void initMap(){ 
     Map<Integer, MonthData> monthDataMap = new HashMap<Integer,MonthData>(); 
     monthDataMap.put(1,new JanData()); 
     monthDataMap.put(2,new FebData()); 

    } 

    interface MonthData { 
     public String getMonth(); 
     public int getDays(); 
     public String getOut(); 
    } 

    class JanData implements MonthData{ 
     private String month ="January"; 
     private int days = 30; 
     private String out = "There are " + days + " days in " + month; 

     @Override 
     public String getMonth() { 
      return month; 
     } 

     @Override 
     public int getDays() { 
      return days; 
     } 

     @Override 
     public String getOut() { 
      return out; 
     } 
    } 


    class FebData { 
     private String month ="February"; 
     private int days = 28; 
     private String out = "There are " + days + " days in " + month; 
    } 

    ....