**一般来讲
运算符或方法 含义**
% 取余,结果符号和被除数一致。
*Math.floorMod() 取模,结果符号和除数一致。
取余和取模的计算规则:c=a/b,r=a-c*b。取余时,c向0取整。取模时,c向负无穷取整。
被除数和除数符号相同时,取余和取。模的结果相同。不同时,结果不同*

   System.out.println(7 % -3);
// 取余:c = 7 / (-3) = -2, r = 7 - (-2) * -3 = 1
System.out.println(Math.floorMod(7, -3));
// 取模:c = 7 / (-3) = -3, r = 7 - (-3) * -3 = -2

在Java中使用

public static void main(String[] args) {
    System.out.println("17/10="+17/10);
    System.out.println("-17/-10="+-17/-10);
    System.out.println("-17/10="+-17/10);
    System.out.println("17/-10="+ 17/-10);
    System.out.println("===============================");
    System.out.println("17%10="+17%10);
    System.out.println("-17%-10="+-17%-10);
    System.out.println("-17%10="+-17%10);
    System.out.println("17%-10="+17%-10);

}

删除上边代码中的\
输出为

17/10=1
-17/-10=1
-17/10=-1
17/-10=-1
===============================
17%10=7
-17%-10=-7
-17%10=-7
17%-10=7
Last modification:October 15th, 2022 at 09:58 pm
如果觉得我的文章对你有用,请随意赞赏