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取整的

你可能感兴趣的:(Java知识点整理)