错误消息:´)´预期,并且“不是声明” ´;´预计
我对编程完全陌生。我在尝试学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
showMessageDialog
旨在显示加法的结果,这一行可能应该读成类似这样的内容
(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
在这种情况下,代码如下所示Assuming the
showMessageDialog
is intended to present the result of the addition, this lineshould probably read something like
(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 produce53 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这就是问题所在:
不清楚你在这里的意思。你真的想要算术和吗?如果是这样,您不应该在
+
运算符周围使用引号:如果您实际上打算使用字符串连接并在字符串中包含“+”,则需要使用一些东西像这样:
看起来有点像这样:
这里的第一个和第三个
+
符号是字符串连接运算符。中间的一个位于字符串文字内。This is the problem:
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:If you actually meant to use string concatenation and include "+" in the string, you need to use something like this:
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.您需要使用字符串 cat 运算符连接字符串: +
Btw;在第一次分配变量的地方声明变量被认为是一件好事,而不是遵守旧的 C 要求(具有古老的根源),即所有变量都必须在实际代码之前声明。
当试图找出变量的使用位置时,它会有点帮助。
当变量仅在特定范围内使用时,它特别好; (在大括号内)
You need to concatenate strings using the string cat operator: +
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.
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)