亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的 Java 代碼“if 語句”出現不需要的輸出(標題太長,無法描述)

我的 Java 代碼“if 語句”出現不需要的輸出(標題太長,無法描述)

慕的地8271018 2024-01-05 15:14:11
編輯:添加更多代碼(對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秒?!?
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

if (n_Days == 0)

這是你的第一個if聲明。如果n_Days0,它將被執行,而且最重要的是 -else語句永遠不會被執行。因此, if n_Daysis0n_Hoursis 0tooif (n_Days == 0)都會被執行,并且else if...- 語句將被跳過。

您只需更改語句的順序 - 或嵌套它們 - 就可以了。


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號