Oddity
你拿到一份產品的不同 ID 清單,這些產品分別用於陸上或海上。你知道陸上產品配的是偶數,海上產品配的是奇數。為了更方便工作,你想知道這份清單中哪些元素是「奇數」。
本練習屬於課程
Java 中級
練習說明
- 「呼叫」你自訂的方法。
- 為你的方法設定正確的
return型別。 - 在方法內用取餘數運算子
%來檢查是否為奇數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
class OddityFinder {
public static void main(String[] args) {
int[] intArray = {32, 67, 86, 90, 26, 34, 92, 13, 70, 50};
for (int number : intArray) {
// Call your custom method
if (____(number)) System.out.print(number + "\n");
}
}
// Give the correct return type for your method
public static ____ isOdd(int n) {
// Use clever math to determine that it is odd
return (____) != 0;
}
}