編輯:添加更多代碼(對(duì)java非常陌生,我沒(méi)有任何教科書(shū)。我正在根據(jù)我在網(wǎng)上找到的內(nèi)容拼湊它是如何工作的,請(qǐng)友善。)我正在嘗試為我在大學(xué)的 Java 課程制作一個(gè)“時(shí)間計(jì)算器”。(這是我們的任務(wù))。我必須像這樣輸出:(天)HH:MM:SS。如果沒(méi)有“天”,則為 HH:MM:SS,如果沒(méi)有“小時(shí)”,則為 MM:SS,依此類(lèi)推。然而,在我的代碼中,我的 if 語(yǔ)句只有幾秒鐘,沒(méi)有分鐘、小時(shí)或天,因此無(wú)法正確執(zhí)行。如果我輸入 40 秒,它將輸出:0:00:40 小時(shí)。我該如何解決?我知道 If/If else 語(yǔ)句有問(wèn)題。我試圖通過(guò)輸入 1-60 之間的各種數(shù)字來(lái)找出問(wèn)題,但它總是返回“0:00:(1-60) 小時(shí)”。我不知道從哪里開(kāi)始解決這個(gè)問(wèn)題。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."); } }}如果我輸入的總秒數(shù)小于 60,那么我希望第二個(gè)“else-if”語(yǔ)句像這樣執(zhí)行:“你輸入了30秒,就是30秒。”
我的 Java 代碼“if 語(yǔ)句”出現(xiàn)不需要的輸出(標(biāo)題太長(zhǎng),無(wú)法描述)
慕的地8271018
2024-01-05 15:14:11