區別:
%是求模運算符,/是除運算符。 %是求餘運算,/是普通的除號。
實例:
System.out.println(3/5) = 0; System.out.println(2/5) = 0; System.out.println(4/5) = 0; System.out.println(6/5) = 1; System.out.println(7/5) = 1; System.out.println(8/5) = 1; System.out.println(11/5) = 2; System.out.println(12/5) = 2; System.out.println(13/5) = 2;
結果等於得到的整數(商的整數)
System.out.println(17%5) = 2; System.out.println(16%5) = 1; System.out.println(13%5) = 3; System.out.println(8%5) = 3; System.out.println(7%5) = 2;
結果等於餘數(剩餘多少)
System.out.println(2%5) = 2; System.out.println(7%8) = 7; System.out.println(6%8) = 6;
取餘(取模)有個規律就是:左邊小於右邊,結果是左邊,左邊大於右邊,看餘數。
推薦教學:java入門教學
以上是java中'/”與'%”的區別的詳細內容。更多資訊請關注PHP中文網其他相關文章!