错误消息:´)´预期,并且“不是声明” ´;´预计

发布于 2024-11-17 18:38:14 字数 3242 浏览 1 评论 0原文

我对 Java 编程完全陌生,并且自己学习了一些基本教程。 我正在尝试解决一个练习,它告诉我制作一个小程序,用户在其中输入几秒钟。然后程序应该返回这是多少小时、分钟和秒。我无法摆脱错误消息。有人可以帮我吗? 我的代码如下,

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

I´m totally new to programming in java, and following some basic tutorials for my self.
I´m trying to solve an excercise wich tells me to make a small program where the user types in an amonunt of seconds. The program is then supposed to return how many hours, minutes and seconds this is. I can´t get ridd of the error messages. Can anyone help me please?
My code is the following

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

I get the following error-message when I try to compile:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身情人 2024-11-24 18:38:14

在每个小时分钟秒2之后,您需要在双引号前添加一个+符号。

After each of hours, minutes and seconds2 you need to add a + sign before the double quotes.

溇涏 2024-11-24 18:38:14

应该是这样的:

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

should be this:

"That will be " + hours + "hours, " + minutes  + "minutes, and " + seconds2 + "seconds."
寄离 2024-11-24 18:38:14

错误消息有点棘手。真正的问题在这里:

seconds2“秒”。
(两个变量之间缺少一个“+”号)

The error-message is a little bit tricky. The real problem is here:

seconds2 "seconds."
(there is a missing "+" sign between the two variables)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文