如何在字符串后添加到println中的数字

问题描述 投票:-2回答:2
System.out.print("Wrong. correct answer is " 5 + 7);

此输出:错误。正确答案是57我希望它输出:错误。正确答案是12

java
2个回答
0
投票

您的问题是,在这种情况下,您将两个字符串组合在一起:字符串5和字符串7。结果为57。如果要加两个数字,请使用以下代码:

int result = 5 + 7;
System.out.print("Wrong. correct answer is " + result);

0
投票

我们也可以使用printf,它也允许格式化。

System.out.printf("Wrong. correct answer is %d", 5 + 7);
© www.soinside.com 2019 - 2024. All rights reserved.