创建一个长度为10的整形数组,并使用随机数赋值.
求出其中的偶数,奇数
偶数的个数的和
奇数的个数的和
1条回答 默认 最新
诗人在流浪 2021-10-25 23:09关注public static void main(String[] args) { int[] arr = new int[10]; Random r = new Random(); int count=0; for(int i=0;i<10;i++){ arr[i] = r.nextInt(100); if(arr[i]%2==0){//符合条件则为偶数 count++; System.out.println(arr[i]+"为偶数"); }else{ System.out.println(arr[i]+"为奇数"); } } System.out.println("偶数的个数为:"+count+",奇数的个数为:"+(10-count)); }输出结果:
66为偶数
12为偶数
83为奇数
70为偶数
8为偶数
63为奇数
48为偶数
29为奇数
92为偶数
93为奇数
偶数的个数为:6,奇数的个数为:4本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用