gpt4 book ai didi

java - 模数未显示正确的值

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

-46 模 7 是 3,但我得到 -4。为什么?

int sa=-46;
int p=7;
System.out.println(sa);//-46
sa=sa%p;
System.out.println(sa);//-4

编辑:

我是这样解决的

(sa) % p + p) % p;

最佳答案

Java % 的定义(余数运算符)

The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "%", which divides one operand by another and returns the remainder as its result.

(参见引用来源 here )

如果你想创建一个模函数,当你要求模 n 时,它只返回 [0, n) 范围内的数字,你必须编写它(简单)。

关于java - 模数未显示正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22582537/

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