繁体   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;

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

尽管如此,这 2 个 println 包括计算但为什么第二个 println 是错误的。谢谢

您正在做的事情会导致错误,因为 + 被视为分隔字符串部分的运算符。 尝试将该部分放在括号之间,例如:

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

这可能会在您从字符串中排除 + 时修复它。 希望这对您有所帮助,并且我的陈述是正确的。

暂无
暂无

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

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