//Jisuan.javapublic class Jisuan{
  public static void main(String[] args){
    int a=-8%3;             //将这里改成 int a=-8%-3,输出结果还是-2
    System.out.println(a);
  }
}
谁能帮忙解释一下??

解决方案 »

  1.   

    见java specification了啦,java  的%只跟分子有关,跟分母无关
      

  2.   

    Java language specification 3ed15.17.3 Remainder Operator %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. If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.Examples:5%3 produces 2                 (note that 5/3 produces 1)
    5%(-3) produces 2               (note that 5/(-3) produces -1)
    (-5)%3 produces -2              (note that (-5)/3 produces -1)
    (-5)%(-3) produces -2           (note that (-5)/(-3) produces 1)