2014-03-29 130 views
0

嗨,這是我用於我的應用程序的代碼。每一件事都可以正常工作,但是在某些情況下,當輸入使用第二種情況的數據時,if-else語句和嵌套的if-else語句顯示不同System.out.print的長答案。我需要做什麼來顯示一個答案?我想程序如何使用帶嵌套if-else語句的switch語句輸出日期

package javaapplication18; 
import java.util.Scanner; 

/** 
* 
* @author Thurston Pietersen 
*/ 
public class JavaApplication18 
{ 
     public static void main(String[] args) 
    { 
     int sI,fI ,sL, month, day, year; 
     String mm, dd, yyyy, date; 
     Scanner keyboard = new Scanner(System.in); 
     // TODO code application logic here 
     System.out.print("Input date using the following format mm/dd/yyyy: "); 
     date = keyboard.next(); 

     sL = date.length(); 

     fI = date.indexOf("/"); 
     sI = date.lastIndexOf("/"); 

     mm = date.substring(0,fI); 
     month = Integer.parseInt(mm); 
     dd = date.substring((fI+1),sI); 
     day = Integer.parseInt(dd); 
     yyyy = date.substring((sI+1),sL); 
     year = Integer.parseInt(yyyy); 


     switch (month) 
     { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 8: 
      case 10: 
      case 12: 
       if (day > 31) 
        System.out.print(date + " is an invalid date."); 
       else 
        System.out.print(date + " is a valid date."); 

      case 4: 
      case 6: 
      case 9: 
      case 11: 
       if (day > 30) 
        System.out.print(date + " is an invalid date."); 
       else 
        System.out.print(date + " is a valid date."); 
      case 2: 
       if (year % 4 == 0) 
        if (day > 29) 
         System.out.print(date + " is an invalid day."); 
        else 
         System.out.print(date + " is a valid date and leap year."); 
       else 
        if (day > 28) 
         System.out.print(date + " is an invalid day."); 
        else 
         System.out.print(date + " is a valid date."); 
      default: 
       System.out.println("The date: " + date + " has an invalid month"); 




     }  



    } 

} 
+0

可能重複[爲什麼我們需要打破案例陳述?](http://stackoverflow.com/questions/2710300/why-do-we-need-break-after-case-statements) – donfuxx

回答

2

你顯示,如果輸入的日期,如果它能夠顯示有效日期,如果輸入的日期是閏年,以及顯示一個錯誤,如果輸入的日期或月份錯誤在所有情況下缺少break。由於這個原因,交換機通過,所有情況都被執行。在適當的地方插入break

switch (month) { 
     case 1: 
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      if (day > 31) 
       System.out.print(date + " is an invalid date."); 
      else 
       System.out.print(date + " is a valid date."); 
      break; // Here 

     case 4: 
     case 6: 
     case 9: 
     case 11: 
      if (day > 30) 
       System.out.print(date + " is an invalid date."); 
      else 
       System.out.print(date + " is a valid date."); 
      break; // Here 

     case 2: 
      if (year % 4 == 0) 
       if (day > 29) 
        System.out.print(date + " is an invalid day."); 
       else 
        System.out.print(date + " is a valid date and leap year."); 
      else 
       if (day > 28) 
        System.out.print(date + " is an invalid day."); 
       else 
        System.out.print(date + " is a valid date."); 
      break; // Here 

     default: 
      System.out.println("The date: " + date + " has an invalid month"); 

    }  

並請始終使用大括號您if S和else個習慣中。