2013-07-26 30 views
-2

我正在做一個需要3個輸入像「1500,1和1」或「1500,1月和1」,並返回「1月1日,1500年」或「1/1/1500「,我在當天遇到了一些問題,但有人已經告訴我如何解決這個問題,現在我在月份部分遇到問題,我做了這個有點快,我還沒有弄清楚爲什麼它不是不工作,它應該看看輸入是否是一個有效的月份,如果它是輸出月份(這部分只是爲了測試),如果不是,那麼它應該說「請使用一個有效的月份或一個有效的月份數字在1和12之間「,但是當我寫任何不是一個月的東西時,它就會停止,並且不輸出任何東西,即使我在一個月後沒有做任何事情,我也試着看看是否有是任何錯誤,但我沒有找到任何,這是我使用的代碼:該代碼不工作出於某種原因

Scanner scan = new Scanner(System.in); 
    String mx; 
    System.out.println("Insert Month"); 
    String[] mm = {"january","february","march","april","may","june","july","august","september","october","november","december"}; 
    int mz = 0; 
    while (0 < 1){ 
    mx = scan.nextLine(); 
     mx = mx.toLowerCase(); 
     for(int i = 0; i < 11; i++){ 
      if (mx.equals(mm[i])){ 
       mz = i + 1; 
       break; 
      } 
      else { 
       if(i == 11){ 
        System.out.println("please use a valid month or a number between 1 and 12"); 
       } 
       else{ 
       } 
      } 
     } 
    //} 
    if(mz > 0){ 
     break; 
    } 
    else {} 
    } 
    System.out.println(mx); 
+4

一年有12個月,所以你應該檢查我<12,而不是11. – Jiminion

+1

爲什麼零星的輸入?爲什麼不讓他們先進入一個月,然後是一天,然後是一年。你應該限制用戶的輸入,以使你的生活更輕鬆 – sunrize920

+0

只需使用for循環這種方式'for(int i = 0; i <= 11; i ++)' –

回答

0

你的程序只是「停止」的原因是,你只能打印聲明「請輸入一個有效的一個月... 「如果i == 11和你有你的for循環中斷,如果i >= 11。因此,這種情況永遠不會得到滿足。 while循環保持運行,即使這個語句不打印。您可能在第一次嘗試時輸入了非月份字符串,然後在第二次輸入了一個月字符串,並且您的while循環將被打破。

以下是我如何改進您的代碼以便在本月進行工作。注意突出顯示的細微變化。這些是寫出更好,更可讀的代碼很重要:

Scanner scan = new Scanner(System.in); 
//initialize to empty string 
String mx = ""; 
System.out.println("Insert Month"); 
//use good naming conventions for easier code readability 
String[] validMonths = {"january","february","march","april","may","june","july","august","september","october","november","december"}; 
//using a boolean to break makes much more sense than the way you have written it with an infinite loop and a manual break statement 
boolean noMonth = true; 
while (noMonth){ 
    mx = scan.nextLine(); 
    for(int i = 0; i < 12; i++){ 
     //rather than convert to lowercase, use this handy String method 
     //also compares for valid number entries 
     if (mx.equalsIgnoreCase(validMonths[i]) || mx.equals(Integer.toString(i+1))){ 
      noMonth = false; 
      break; 
     } 
    } 
    if(noMonth){ 
     System.out.println("please use a valid month or a number between 1 and 12"); 
    } 
} 
System.out.println(mx); 

創建一個新的循環,同時在白天和一個新採取這些後一年,檢查有效的輸入需要。另外,每個if都不需要Java中的其他東西。

+0

它是否適用於'Decembermber'? –

+0

在OP代碼中沒有看到該錯誤。好決定 – sunrize920

0

你沒有使用有意義的變量名,使你的代碼有點難以閱讀和維護。所以,我不得不從頭開始創建你下面的代碼:

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

     String month = getMonthName(getInt("Enter Month: ", keyboard) - 1); 
     int day = getInt("Enter Day: ", keyboard); 
     int year = getInt("Enter Year: ", keyboard); 

     System.out.printf("%s %d, %d\n", month, day, year); 

} 

public static String getMonthName(final int monthNo) 
{ 
     String[] months = {"january","february","march","april","may","june","july","august","september","october","november","december"}; 
     return months[monthNo]; 
} 


public static int getInt(final String msg, Scanner keyboard) 
{ 
     System.out.print(msg); 
     return keyboard.nextInt(); 
} 

上面的代碼不執行,並輸入驗證,你可能已經注意到。如果您想驗證例如一個月的輸入,如果你的情況可能是這個樣子:

if (month < 0 || month < 12) 
{ 
System.out.println("Invalid month number entered");  
System.exit(0); 
} 
+0

您需要處理輸入錯誤。如果輸入不是int,nextInt會拋出一個錯誤 – sunrize920

+0

就像我在上次編輯中所說的,代碼不會執行任何輸入驗證。他似乎很難弄清楚他的程序的邏輯,而不是編寫代碼。當然,如果他需要處理每一個可能的錯誤,try-catch語句將會這樣做(我個人非常反感)。 –