HomeWork10.java 737 B

123456789101112131415161718192021
  1. /**
  2. * @author WanJl
  3. * @version 1.0
  4. * @title HomeWork10
  5. * @description 7月15日作业:定义一个整数数组 `{12, 7, 9, 24, 15, 8, 31, 46}`,遍历数组,统计其中奇数和偶数的个数。
  6. * @create 2026/7/16
  7. */
  8. public class HomeWork10 {
  9. public static void main(String[] args) {
  10. int[] arr={12, 7, 9, 24, 15, 8, 31, 46};
  11. int count=0; //作为奇数个数
  12. //遍历数组
  13. for (int i = 0; i < arr.length; i++) {
  14. //如果某个数组元素余2不等于0,就说明是奇数
  15. if (arr[i]%2!=0){
  16. count++; //就累加1
  17. }
  18. }
  19. System.out.println("奇数个数:"+count+",偶数个数:"+(arr.length-count));
  20. }
  21. }