2016-09-28 70 views
-1

所以我正在寫一個程序,給出每個輸入x秒鐘的分鐘數...現在的問題是,一旦我輸入第一個值,它會要求另一個值並將其分開....並且另一個價值......另一個價值......等等......我怎樣才能得到它只給我一個價值,並完成一個價值而不是永無止境的事物?重複JOptionPane彈出?

import javax.swing.JOptionPane; 

class TimeCalculator { 
public static void main(String[] args) { 

double seconds; 
String input; 

input = JOptionPane.showInputDialog("Enter any number of seconds"); 

seconds = Double.parseDouble(input); 

if (seconds >= 60); 
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds."); 


if (seconds >= 3600); 
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds."); 

if (seconds >= 86400); 
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds."); 

System.exit(0); 




} 
} 
+0

「'if(seconds> = 60);'」你確定這是正確的嗎? – 2016-09-28 18:09:30

+0

使用'JOptionPane.showMessageDialog()'消息。 https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html – 2016-09-28 18:12:45

回答

0

首先 - 刪除每個if語句後的分號。其次,當您不要求輸入時,將showInputDialog更改爲showMessageDialog。第三,糾正你的代碼的邏輯:

class TimeCalculator{ 

    public static void main(String[] args) { 
     double seconds; 
     String input; 

     input = JOptionPane.showInputDialog("Enter any number of seconds"); 

     seconds = Double.parseDouble(input); 

     if (seconds >= 60) 
      JOptionPane.showMessageDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds."); 

     if (seconds >= 3600) 
      JOptionPane.showMessageDialog(null, "There are " + (seconds/3600) + " hours in " + seconds + " seconds."); 

     if (seconds >= 86400) 
      JOptionPane.showMessageDialog(null, 
        "There are " + (seconds/86400) + " days in " + seconds + " seconds."); 
     System.exit(0); 
    } 
} 
+0

我刪除了分號,它沒有改變任何東西 – Daoud

+0

@Daoud我編輯了我的答案。如果有幫助,請接受它作爲正確答案。 –

+0

所以我做了這個,當我跑它時,出現了4個錯誤,表示「無法找到符號」,並指向J中的JOptionPane – Daoud