Bắt đầu ngayBắt đầu miễn phí

Oddity

You have been given the list of different IDs for products meant for either land or sea. You know that land products are given an even number and sea products have odd one. To facilitate your job, you want to know which elements are odd in this list.

Bài tập này là một phần của khóa học

Intermediate Java

Xem khóa học

Hướng dẫn bài tập

  • Call your custom method.
  • Give the correct return type for your method.
  • Inside the method, use the modulo operator, %, to check for odd numbers.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

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;
  }
}
Chỉnh sửa và Chạy Mã