9_Java中两个int相除向哪个方向取整

  •   class Solution {
    
          public static void main(String[] args) {
    
              int result = 0;
    
              result = 4/3;
              System.out.println("4 / 3: " + result);
    
              result = -4/-3;
              System.out.println("-4 / -3: " + result);
    
              result = -4/3;
              System.out.println("-4 / 3: " + result);
    
              result = 4/-3;
              System.out.println("4 / -3: " + result);
          }
      }
    

    以上这段代码的结果是

      4 / 3: 1
      -4 / -3: 1
      -4 / 3: -1
      4 / -3: -1
    

    所以结论是: 无论正数还是负数相除, 结果总是向0取整的

猜你喜欢

转载自blog.csdn.net/captxb/article/details/87633646