2017-08-28 68 views
-3

我是一名初學者學習Java的人,我試圖編寫一個程序將用戶輸入的時間轉換爲12小時的時間,或者如果它是以12小時時間格式提供,將其轉換爲24小時時間。24小時制轉換爲12小時制 - 大括號配對問題

我已經寫了一些代碼,這些代碼在我一步步測試的過程中起作用,直到我嘗試修改時間從12小時轉換爲24小時格式。

我的代碼如下。我爲裁員和非常低效的技術表示歉意,但是,嘿,我必須從某個地方開始。我相信我的問題在於分隔代碼塊,因爲我試圖使用主要的if語句來測試輸入是否以'm'結尾(即,如果它在輸入時在12小時或24小時內),然後幾個嵌套if,elseelse if聲明。

import java.util.Scanner; 

public class TimeConverter 
{ 

    public static void main (String [] args) 
{ 

    Scanner ask_user = new Scanner (System.in); 
    System.out.println("Enter a time ([h]h:mm [am|pm]): "); 
    String enter_time = ask_user.nextLine(); 
    String am_pm = enter_time.substring(6); 
    String am = ("am"); 
    String pm = ("pm"); 
    if (enter_time.substring(7).equals("m")) 
{ 
    if (am_pm.equals(am)) 
{ 
    String am_12 = enter_time.substring(0, 2); 
    String mins = enter_time.substring(2,5); 
    int am_12i = Integer.parseInt(am_12); 
    if (am_12i != 12) 
{ 
    String am_sub = enter_time.substring(0,5); 
    System.out.println(am_sub); 


} 
     else if (am_12i == 12) 
{ 
     System.out.println("00" + mins); 
} 

} 

     else if (am_pm.equals(pm)) 
{ 
     if (enter_time.equals("12:00 pm")) 
{ 
     System.out.println(enter_time); 
} 
     else 
{ 
     String minutes = enter_time.substring(2,5); 
     String pm_add = enter_time.substring(0,2); 
     int pm_add_i = Integer.parseInt(pm_add); 
     int pm_add_fin = pm_add_i + 12; 
     String pm_add_finS = Integer.toString(pm_add_fin); 
     String converted_pmtime = (pm_add_finS + minutes); 
     System.out.println(converted_pmtime); 
} 


     else if (enter_time.substring(7) != ("m")) 
{ 
     String 24hour = enter_time.substring(0,2); 
     String 12hourmins = enter_time.substring(2,7); 
     int 24hournum = Integer.parseint(24hour); 
     if (enter_time.equals("00:00")) 
{ 
           System.out.println("12" + 12hourmins); 
} 
     else if (24hournum <= 11) 
{ 
     String hour = Integer.toString(24hournum); 
     String minute = enter_time.substring(2,4); 
     String fin = (hour + minute + "am"); 
} 

}  
} 
} 
+2

學習縮進你的代碼。它會**真的幫助你 – litelite

+1

你錯過了幾個大括號和變量名不能包含數字(24小時是一個無效的變量名)。從那些開始。 –

+1

不要使用==比較字符串。 「else if(enter_time.substring(7)!=(「m」))「在其他情況下實際上是多餘的,但如果您堅持,請將!=替換爲!equals –

回答

2

真正的問題是,您沒有正確縮進代碼。一旦你這樣做了,你自己就可以更容易地發現問題了。

一些錯誤,我從剛趕緊看你的代碼中發現:

  1. 你缺少幾個括號。 (同樣,正確的縮進將對此有所幫助。)

    if (enter_time.equals("12:00 pm")) 
    { 
        System.out.println(enter_time); 
    } 
    else 
    { 
        String minutes = enter_time.substring(2,5); 
        String pm_add = enter_time.substring(0,2); 
        int pm_add_i = Integer.parseInt(pm_add); 
        int pm_add_fin = pm_add_i + 12; 
        String pm_add_finS = Integer.toString(pm_add_fin); 
        String converted_pmtime = (pm_add_finS + minutes); 
        System.out.println(converted_pmtime); 
    } 
    
    
    else if (enter_time.substring(7) != ("m")) 
    { 
    

    您有一個if聲明。然後用else聲明結束該塊。然而,在那之後else if聲明跟在else之後。 else if語句需要放在if語句之後和else語句之前。或者,elseelse if語句的順序是混淆的,或者您在else聲明和else if聲明部分實際上應該是前一個塊的一部分後缺少結尾括號}

    您的代碼最後還缺少一個右括號。

  2. 變量不能以數字開頭。像24hournum24hour這樣的所有變量都是無效的。

  3. 在以下行int 24hournum = Integer.parseint(24hour);中,您使用parseint而不是parseInt

我的建議是:

  1. 正確縮進所有代碼。大多數IDE都帶有快捷方式來輕鬆完成此操作。
  2. 閱讀並研究控制檯中顯示的所有錯誤和警告消息。
  3. 調試並逐步通過您的代碼來查找導致錯誤的部分。
相關問題