简体   繁体   English

Java,int x = 5; System.out.println(" x + 5 是 " + x + 5); System.out.println("x += 5 是 " + x += 5); 为什么第二个 println 是错误的?

[英]Java ,int x = 5; System.out.println(" x + 5 is " + x + 5); System.out.println("x += 5 is " + x += 5); why second println is mistake?

Java int x = 5; Java int x = 5;

    System.out.println(" x + 5 is " + x + 5);//correct
    System.out.println("x += 5 is " + x += 5);// why wrong?

Even though, these 2 println is including calculation but why second println is error.Thanks尽管如此,这 2 个 println 包括计算但为什么第二个 println 是错误的。谢谢

What you are doing causes an error because the + is seen as an operator to seperate parts of the string.您正在做的事情会导致错误,因为 + 被视为分隔字符串部分的运算符。 Try placing that part between brackets like:尝试将该部分放在括号之间,例如:

System.out.println("x += 5 is " + (x += 5));

This might fix it as you exclude the + from the string.这可能会在您从字符串中排除 + 时修复它。 Hope this helps you a bit, and that I am correct in my statement.希望这对您有所帮助,并且我的陈述是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM