gpt4 book ai didi

java - 在 Java 中使用 % 运算符

转载 作者:行者123 更新时间:2023-12-01 14:11:25 25 4
gpt4 key购买 nike

为什么下面我得到的是 2 而不是 -2?

System.out.println(11%-3);

考虑到:

System.out.println(-11%3);

返回-2..

提前致谢。

最佳答案

引用JLS :

The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.

<小时/>

It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

(强调我的。)

另外,引用Modulo operation有关各种编程语言中的模运算符的信息。

关于java - 在 Java 中使用 % 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18504954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com