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

发布于 2024-11-17 22:55:41 字数 1561 浏览 3 评论 0原文

我对编程完全陌生。我在尝试学习 Java 编程时,正在阅读互联网上的一些教程以及 Barry Burds 的“Java for dummies”。我已经尝试了所有我能想到的变化但没有成功。 在一个练习中,我应该编写以下程序来在“messageDialogBox”中打印一条消息,其中应包含程序用户写入的数字。不幸的是,我在尝试编译时收到以下错误消息:有人可以帮助我让代码正常工作吗?代码有什么问题吗?

5 errors
Addition2.java:24: ')' expected
      JOptionPane.showMessageDialog( null, firstnumber "+" secondnumber
                                                      ^
Addition2.java:25: not a statement
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
      ^
Addition2.java:25: ';' expected
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
           ^
Addition2.java:25: not a statement
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
                                   ^
Addition2.java:25: ';' expected
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );

代码如下:

import javax.swing.JOptionPane;

public class Addition2
{
   public static void main( String args[] )
   {
      String firstnumberstring;
      String secondnumberstring;

      int firstnumber;
      int secondnumber;
      int sum;

      firstnumberstring = JOptionPane.showInputDialog( 
            "Write first number" );
      secondnumberstring =
         JOptionPane.showInputDialog( "Write second number" );

      firstnumber = Integer.parseInt( firstnumberstring );
      secondnumber = Integer.parseInt( secondnumberstring );

      sum = firstnumber + secondnumber;

      JOptionPane.showMessageDialog( null, firstnumber "+" secondnumber
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
   } 
}

I am totally new to programming. I´m reading some tutorials on the internet, and also Barry Burds "Java for dummies" while trying to learn programming Java. I have tried all variations I could think of without success.
In one exercise I am supposed to make the following program to print a message in a "messageDialogBox" which should contain the numbers wrote in by the user of the program. Unfortunately I get thhe following error message when trying to compile: Can somebody please help me get the code to work? What is wrong with the code?

5 errors
Addition2.java:24: ')' expected
      JOptionPane.showMessageDialog( null, firstnumber "+" secondnumber
                                                      ^
Addition2.java:25: not a statement
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
      ^
Addition2.java:25: ';' expected
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
           ^
Addition2.java:25: not a statement
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
                                   ^
Addition2.java:25: ';' expected
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );

The code is the following:

import javax.swing.JOptionPane;

public class Addition2
{
   public static void main( String args[] )
   {
      String firstnumberstring;
      String secondnumberstring;

      int firstnumber;
      int secondnumber;
      int sum;

      firstnumberstring = JOptionPane.showInputDialog( 
            "Write first number" );
      secondnumberstring =
         JOptionPane.showInputDialog( "Write second number" );

      firstnumber = Integer.parseInt( firstnumberstring );
      secondnumber = Integer.parseInt( secondnumberstring );

      sum = firstnumber + secondnumber;

      JOptionPane.showMessageDialog( null, firstnumber "+" secondnumber
      + sum, "Results", JOptionPane.PLAIN_MESSAGE );
   } 
}

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

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

发布评论

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

评论(3

送君千里 2024-11-24 22:55:41

假设 showMessageDialog 旨在显示加法的结果,这一行

JOptionPane.showMessageDialog(null, firstnumber "+" secondnumber
      + sum, "Results", JOptionPane.PLAIN_MESSAGE);

可能应该读成类似这样的内容

JOptionPane.showMessageDialog(null, firstnumber + "+" + secondnumber + "=" +
                                                ^     ^              ^^^^^^^

      + sum, "Results", JOptionPane.PLAIN_MESSAGE);

(Just put strings and numerics around other other does not concatenate them. You'll have to put a < code>+ 位于其间!)

另外,请注意 5 + 3 + " hello" 会产生 "8 hello"。要生成 53 hello,您必须执行 "" + 5 + 3 + " hello" 等操作。


您还可以使用例如 String.format 在这种情况下,代码如下所示

String msg = String.format("%d + %d = %d", firstnumber, secondnumber, sum);
JOptionPane.showMessageDialog(null, msg, "Results", JOptionPane.PLAIN_MESSAGE);

Assuming the showMessageDialog is intended to present the result of the addition, this line

JOptionPane.showMessageDialog(null, firstnumber "+" secondnumber
      + sum, "Results", JOptionPane.PLAIN_MESSAGE);

should probably read something like

JOptionPane.showMessageDialog(null, firstnumber + "+" + secondnumber + "=" +
                                                ^     ^              ^^^^^^^

      + sum, "Results", JOptionPane.PLAIN_MESSAGE);

(Just putting strings and numbers beside each other doesn't concatenate them. You'll have to put a + in between!)

Also, note that 5 + 3 + " hello" yields "8 hello". To produce 53 hello, you'd have to do for instance "" + 5 + 3 + " hello".


You could also use for instance String.format in which case the code would look like

String msg = String.format("%d + %d = %d", firstnumber, secondnumber, sum);
JOptionPane.showMessageDialog(null, msg, "Results", JOptionPane.PLAIN_MESSAGE);
神也荒唐 2024-11-24 22:55:41

这就是问题所在:

firstnumber "+" secondnumber

不清楚你在这里的意思。你真的想要算术和吗?如果是这样,您不应该在 + 运算符周围使用引号:

JOptionPane.showMessageDialog(null, firstnumber + secondnumber + sum,
    "Results", JOptionPane.PLAIN_MESSAGE );

如果您实际上打算使用字符串连接并在字符串中包含“+”,则需要使用一些东西像这样:

JOptionPane.showMessageDialog(null, firstnumber + "+" + secondnumber + "=" + sum,
    "Results", JOptionPane.PLAIN_MESSAGE );

看起来有点像这样:

+ "+" +

这里的第一个和第三个 + 符号是字符串连接运算符。中间的一个位于字符串文字内。

This is the problem:

firstnumber "+" secondnumber

It's not clear what you mean here. Did you actually want an arithmetic sum? If so, you shouldn't have quotes around the + operator:

JOptionPane.showMessageDialog(null, firstnumber + secondnumber + sum,
    "Results", JOptionPane.PLAIN_MESSAGE );

If you actually meant to use string concatenation and include "+" in the string, you need to use something like this:

JOptionPane.showMessageDialog(null, firstnumber + "+" + secondnumber + "=" + sum,
    "Results", JOptionPane.PLAIN_MESSAGE );

Looking at a bit like this:

+ "+" +

the first and third + signs here are the string concatenation operator. The middle one is within a string literal.

静谧 2024-11-24 22:55:41

您需要使用字符串 cat 运算符连接字符串: +

JOptionPane.showMessageDialog( null, firstnumber  + "+" + secondnumber + " = " + sum, "Results", JOptionPane.PLAIN_MESSAGE );

Btw;在第一次分配变量的地方声明变量被认为是一件好事,而不是遵守旧的 C 要求(具有古老的根源),即所有变量都必须在实际代码之前声明。

String firstnumberstring = JOptionPane.showInputDialog( 
        "Write first number" );

当试图找出变量的使用位置时,它会有点帮助。
当变量仅在特定范围内使用时,它特别好; (在大括号内)

You need to concatenate strings using the string cat operator: +

JOptionPane.showMessageDialog( null, firstnumber  + "+" + secondnumber + " = " + sum, "Results", JOptionPane.PLAIN_MESSAGE );

Btw; it's considered a good thing to declare variables where they are assigned for the first time - and not sticking to the old c- requirement (with ancient roots) that all variables must be declared before the actual code.

String firstnumberstring = JOptionPane.showInputDialog( 
        "Write first number" );

It helps a little when trying to figure out where a variable is used.
It's particulary good when a variable is used only in a specific scope; ( inside curly brackets)

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