一條沒看懂的語句!
?""x""為什么結果顯示為"4"主要是這條語句""int x=(m*8/(n+2))%m;"沒看明白"
public class HelloWorld {
? ? public static void main(String[] args) {
int m = 5;
int n = 7;
int x=(m*8/(n+2))%m;
System.out.println("m:" + m);
System.out.println("n:" + n);
System.out.println("x:" + x);
}
}
2017-06-29
int m = 5;
int n = 7;
int x=(m*8/(n+2))%m;
有括號先算括號里的內容,和數學里的算法順序一樣。先算(n+2)=9,再算m*8=40,再算m*8/(n+2)=4,除法取商;再算(m*8/(n+2))%m=4,%表示除法取余數。
2017-06-28
m*8=40
40/9=4(取整)
4%5=4(求余數)
2017-06-28
/是取商
2017-06-28
m*8=40;
40/9=4;取整
4%5=4;取余
2017-06-28
5*8/9)%5=(40/9)%5=4%5=4
40/9=4.4444,因為定義的是整數型所以運算結果只取4整數。
4%5=0余4,所以運算結果是4.