Java程序从数组打印奇数和偶数
在循环检查中,对每个元素进行I%2运算的结果,如果元素为0,则元素为偶数,否则元素为奇数。
示例
public class OddNumbersInAnArray {public static void main(String args[]){
int[] myArray = {23, 93, 56, 92, 39};
System.out.println("Even numbers in the given array are:: ");
for (int i=0; i<myArray.length; i++){
if(myArray[i]%2 == 0){
System.out.println(myArray[i]);
}
}
System.out.println("Odd numbers in the given array are:: ");
for (int i=0; i<myArray.length; i++){
if(myArray[i]%2 != 0){
System.out.println(myArray[i]);
}
}
}
}
输出结果
Even numbers in the given array are::56
92
Odd numbers in the given array are::
23
93
39
以上是 Java程序从数组打印奇数和偶数 的全部内容, 来源链接: utcz.com/z/322404.html