java 3*012=30,而不是36。为什么呢?
我很困惑这是为什么,我似乎找不到答案。这是作业中的内容:
x=1, y=2, z=3;
z=(int)(x/y*3.0+z*012);
System.out.printf("%d %d %d", x, y, z);
答案是:
1 2 30; << from eclipse
我是如何来到这里的:
(1/2) = 0 * 3.0 = 0 + (z*012)= 30
。我想说36,但根据IDE,我猜是30
你可以在下面搜索框中键入要查询的问题!
我很困惑这是为什么,我似乎找不到答案。这是作业中的内容:
x=1, y=2, z=3;
z=(int)(x/y*3.0+z*012);
System.out.printf("%d %d %d", x, y, z);
答案是:
1 2 30; << from eclipse
我是如何来到这里的:
(1/2) = 0 * 3.0 = 0 + (z*012)= 30
。我想说36,但根据IDE,我猜是30
# 1 楼答案
在Java和其他几种语言中,以
0
开头的整数文本被解释为八进制(base 8
)量。这里012
是一个八进制数,它有一个十进制值f10
所以你的乘法会是这样的
从JLS
# 2 楼答案
012
是一个八进制,因为它以0
开头:因此:
注意事项:
0x
开头,例如:0x12 = 1*16 + 2 = 18
# 3 楼答案
见JLS:
所以
012=0*82+1*81+2*80=10
在Java7中,可以使用underscores in numeric literals,这可能有助于中断值
# 4 楼答案
012
是八进制数而不是十进制数,十进制值为10等于
以0开头的数字是八进制数
以0x开头的数字是十六进制数
数字以0b开头,或者OB是二进制数。(自Java第7版起)