5
A
回答
24
%
確實在Java中求餘運算。
要得到適當的彈性模量,可以使用剩餘的功能:
它使用三元操作做標誌修正最短:
private int mod(int x, int y)
{
int result = x % y;
return result < 0? result + y : result;
}
對於那些誰不喜歡三元運算符,這是等效的:
private int mod(int x, int y)
{
int result = x % y;
if (result < 0)
result += y;
return result;
}
8
因爲如果您將-2除以6,則會得到-2作爲餘數。 %運算符將會像下面一樣給出餘數;
int remainder = 7 % 3; // will give 1
int remainder2 = 6 % 2; // will give 0
要得到模:
// gives m (mod n)
public int modulo(int m, int n){
int mod = m % n ;
return (mod < 0) ? mod + n : mod;
}
+0
好的,但是我怎樣才能在Android中獲得mod? – 2012-02-08 22:42:07
相關問題
- 1. 超載運營商<<
- 2. 運營商<<超載
- 3. 運營商<<重載
- 4. 虛擬<<運營商
- 5. 運營商<<在C#
- 6. Ruby:<< - 運營商
- 7. 全球運營商<<和成員運營商之間的共存<<
- 8. 模板運營商<<變圓了
- 9. 運營商<<(ostream&os,...)模板類
- 10. 錯誤不符合運營商<<
- 11. 即使我超載`運營商<<`
- 12. 我想超載運營商<<
- 13. 整數推廣與運營商<<
- 14. 超負荷運營商<<類模板
- 15. 運營商<<重載在C++中
- 16. 運營商<< and >>重載
- 17. ostream的deferencing運營商<<
- 18. <<運營商紅寶石
- 19. 運營商<<重載 - C++和Java
- 20. stringstream的運營商<<崩潰(malloc_consolidate)
- 21. 運營商的解釋<< overload
- 22. 可以專門運營商<<?
- 23. 運營商<<在派生類C++
- 24. 主叫運營商<< in gdb
- 25. 的Javascript運營商<< and >>
- 26. 禁止運營商<<致電
- 27. ActiveRecord的鏟運營商<<
- 28. 曖昧運營商<<選擇
- 29. <a href=""></a>不工作
- 30. 錯誤:對不明確的超負荷「運營商<<」
謝謝唐,它的工作原理。 – 2012-02-08 22:47:15
@don roby:這個操作符意味着什麼:返回結果<0?結果+ y:結果;謝謝 – Kenji 2014-02-26 12:22:21
@Kenji - 請參閱[wikipedia on ternary operator?:](http://en.wikipedia.org/wiki/%3F :) – 2014-02-26 12:47:35