有人可以解释为什么添加空单引号(在此代码的末尾)会创建32吗?我已经改变了整数3和4,每次这样做时,基数似乎是32。
public static void main(String[] args) {
int number = 5;
System.out.println("Initial Value: " + number);
number = number*2;
number = number*2;
System.out.println("\n1. After doubling it twice: " + number);
number = number + 3;
number += 3;
System.out.println("\n2. After adding 3 twice: " + number);
number -= 12;
System.out.println("\n3. After subtracting 12: " + number);
number = number / 3;
System.out.println("\n4. After dividing by 3: " + number);
System.out.println();
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
System.out.println("\n5. After adding 1 four times: " + number);
number -= 1;
System.out.println("\n6. After decrementing once: " + number);
int remainder = number%= 3;;
System.out.println("\n7. Remainder when dividing by 3 is :" + remainder);
int a = 2, b = 3, c = 5;
double d1, d2, d3, d4;
d1 = a + b * c / 2;
d2 = (a + b * c) / 2;
d3 = (a + b) * c / 2;
d4 = (a + b) * (c / 2);
System.out.println("\n8. Values: " + d1 + " : " + d2 + " : " + d3
+ " : " + d4);
int p, q;
p = 10;
q = 10;
p += q++;
System.out.println("\n9. Result is: " + (p + q));
double d7 = 4.3, d8 = 34.7;
double truncatedSum = (4.3 + 34.7);
System.out.println("\n10. Sum is " + truncatedSum);
System.out.println("\n11.");
System.out.println("fred " + 3 + 4);
System.out.println(3 + 4 + " fred");
System.out.println("" + 3 + 4);
**System.out.println(' ' + 3 + 4);**
}
输出
初始值:5
两次加倍后:20
两次加入3次后:26
减去12:14后
除以3:4
加1:5 加1:6 加1:7 加1:8
添加1次后4次:8
减少一次后:7
除以3时的余数为:1
值:9.0:8.0:12.0:10.0
结果是:31
Sum为39.0
11。
弗雷德34
7弗雷德
34个
39
答案 0 :(得分:2)
这里的东西
System.out.println(' ' + 3 + 4);
' '
是char类型,是一个数字data type in Java:
char:char数据类型是一个16位Unicode字符。它的最小值为' \ u0000' (或0)和' \ uffff'的最大值(或65,535包括在内)。
,它对应于UNICODE图表中空格字符的数值,例如32.
因此,您可以在输出中看到此计算结果为32 + 3 + 4或39。
答案 1 :(得分:0)
' ' + 3 + 4
因为char
是Java中的数字类型,' '
代表空格字符int值
ASCII中的空格为32,因此结果为:
32 + 3 + 4 = 39