編輯:添加更多代碼(對java非常陌生,我沒有任何教科書。我正在根據我在網上找到的內容拼湊它是如何工作的,請友善。)我正在嘗試為我在大學的 Java 課程制作一個“時間計算器”。(這是我們的任務)。我必須像這樣輸出:(天)HH:MM:SS。如果沒有“天”,則為 HH:MM:SS,如果沒有“小時”,則為 MM:SS,依此類推。然而,在我的代碼中,我的 if 語句只有幾秒鐘,沒有分鐘、小時或天,因此無法正確執行。如果我輸入 40 秒,它將輸出:0:00:40 小時。我該如何解決?我知道 If/If else 語句有問題。我試圖通過輸入 1-60 之間的各種數字來找出問題,但它總是返回“0:00:(1-60) 小時”。我不知道從哪里開始解決這個問題。final int x = 9;final int n_Days;final int n_Hours;final int n_Minutes;final int n_Seconds;n_Days = total_seconds / 86400;n_Hours = (total_seconds % 86400 ) / 3600;n_Minutes = ((total_seconds % 86400 ) % 3600 ) / 60;n_Seconds = ((total_seconds % 86400 ) % 3600 ) % 60;if (n_Days == 0) { if (n_Minutes < x || n_Seconds < x) { String padded = String.format("%02d" , n_Minutes); String padded2 = String.format("%02d" , n_Seconds); System.out.print("You entered " + total_seconds + " seconds, which is " + n_Hours + " hours, " + n_Minutes + " minutes, and " + n_Seconds + " seconds."); System.out.print("\n"); System.out.print(n_Hours + ":" + padded + ":" + padded2 + " hours."); }}else if (n_Days == 0 && n_Hours == 0) { if (n_Minutes < x || n_Seconds < x) { String padded = String.format("%02d" , n_Minutes); String padded2 = String.format("%02d" , n_Seconds); System.out.print("You entered " + total_seconds + " seconds, which is " + n_Minutes + " minutes, and " + n_Seconds + " seconds."); System.out.print("\n"); System.out.print(padded + ":" + padded2); }}else if (n_Days == 0 && n_Hours == 0 && n_Minutes == 0) { System.out.print("You entered " + total_seconds + " seconds, which is " + n_Seconds + " seconds."); } }}如果我輸入的總秒數小于 60,那么我希望第二個“else-if”語句像這樣執行:“你輸入了30秒,就是30秒?!?
我的 Java 代碼“if 語句”出現不需要的輸出(標題太長,無法描述)
慕的地8271018
2024-01-05 15:14:11