2011-06-30 194 views
0

我完全不熟悉Java編程,以及一些基本的教程。 我試圖解決一個練習,它告訴我做一個小程序,用戶輸入一個秒鐘。然後該程序應該返回這是多少小時,幾分鐘和幾秒鐘。我無法擺脫錯誤信息。任何人都可以幫助我嗎? 我的代碼如下錯誤信息:')'expected,and「not a statement'''''expected

import javax.swing.JOptionPane; 
public class Time2 
{ 
    public static void main(String args[]) 
    { 
     // Defining types of data: 
     String secondstring; 
     int minutes; 
     int seconds; 
     int hours; 
     int seconds1; 
     int seconds2; 

     // Making inputwindow and initializing the variable sekondstring: 
     secondstring = JOptionPane.showInputDialog("Type in seconds!"); 

     // Converting secondstring to type int: 
     seconds = Integer.parseInt(secondstring); 

     // Initializing the variables seconds, minutes and hour: 
     hours = seconds/3600; 
     seconds1 = seconds % 3600; 
     minutes = seconds1/60; 
     seconds2 = seconds1 % 60; 

     // Making output box: 
     JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 

    } // End of main method. 
} // End of class Time2 

我收到以下錯誤消息時,我嘗試編譯:

Time2.java:28: ')' expected 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                   ^
Time2.java:28: not a statement 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                     ^
Time2.java:28: ';' expected 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                        ^
Time2.java:28: not a statement 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                            ^
Time2.java:28: ';' expected 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                              ^
Time2.java:28: not a statement 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                                       ^
Time2.java:28: ';' expected 
    JOptionPane.showMessageDialog(null, "That will be " + hours "hours, " + minutes "minutes, and " + seconds2 "seconds.", "Result", JOptionPane.PLAIN_MESSAGE); 
                                          ^
7 errors 

回答

4

每個hoursminutesseconds2你需要前添加一個+標誌雙引號。

0

錯誤信息有點棘手。真正的問題在這裏:

秒2「秒」。 (有兩個變量之間缺少 「+」 號)

2

應該是這樣的:

"That will be " + hours + "hours, " + minutes + "minutes, and " + seconds2 + "seconds." 
+0

謝謝!這解決了問題! :-) – user820913

相關問題