我是java的编程新手,并为我自己做了一些基础教程。 我试图解决一个练习,告诉我做一个小程序,用户键入一个几秒钟的amonunt。然后该程序应该返回多少小时,分钟和秒。我无法理解错误消息。有人可以帮我吗? 我的代码如下
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
答案 0 :(得分:4)
在每个hours
,minutes
和seconds2
之后,您需要在双引号前添加+
符号。
答案 1 :(得分:2)
应该是这样的:
"That will be " + hours + "hours, " + minutes + "minutes, and " + seconds2 + "seconds."
答案 2 :(得分:1)
错误消息有点棘手。真正的问题在于:
秒2“秒。” (两个变量之间缺少“+”符号)