/ 是除运算符,
%是取模运算符
区别:
/ 是普通的除法运算,如果除数和被除数都是整数,则商是取整
%是求余数
private static void test() { System.out.println("2/5 = " + 2 / 5); System.out.println("2%5 = " + 2 % 5); System.out.println("5/2 = " + 5 / 2); System.out.println("5%2 = " + 5 % 2); System.out.println("-5/2 = " + -5 / 2); System.out.println("-5%2 = " + -5 % 2); System.out.println("5/-2 = " + 5 / -2); System.out.println("5%-2 = " + 5 % -2); System.out.println("--------"); System.out.println("2/5.0 = " + 2 / 5.0); System.out.println("2%5.0 = " + 2 % 5.0); System.out.println("5.0/2 = " + 5.0 / 2); System.out.println("5.0%2 = " + 5.0 % 2); System.out.println("-5.0/2 = " + -5.0 / 2); System.out.println("-5.0%2 = " + -5.0 % 2); System.out.println("5.0/-2 = " + 5.0 / -2); System.out.println("5.0%-2 = " + 5.0 % -2); } 运行结果: 2/5 = 0 2%5 = 2 5/2 = 2 5%2 = 1 -5/2 = -2 -5%2 = -1 5/-2 = -2 5%-2 = 1 -------- 2/5.0 = 0.4 2%5.0 = 2.0 5.0/2 = 2.5 5.0%2 = 1.0 -5.0/2 = -2.5 -5.0%2 = -1.0 5.0/-2 = -2.5 5.0%-2 = 1.0