Java:两个整数的总和被打印为两者的串联 [英] Java: sum of two integers being printed as concatenation of the two
本文介绍了Java:两个整数的总和被打印为两者的串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class giri{
public static void main(String args[])
{
int x = 17;
int y = 013;
System.out.println("x+y = " + x+y);
}
}
当我运行程序时,我得到输出1711。任何人都可以告诉我如何获得1711
When I run the program I get the output 1711. Can anybody tell me How do I get 1711
推荐答案
17
是直接在那里。
013
是八进制常量,等于 11
十进制。
013
is an octal constant equal to 11
in decimal.
013 = 1*8 + 3*1 = 8 + 3 = 11
在字符串后添加它们时,它们被串联为字符串,而不是作为数字添加。
When added together after a string, they are concatenated as strings, not added as numbers.
我认为你想要的是:
int x = 17;
int y = 013;
int z = x + y;
System.out.println("x+y = " + z);
或
System.out.println("x+y = " + (x + y));
这将是一个更好的结果。
Which will be a better result.
这篇关于Java:两个整数的总和被打印为两者的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文