JAVA定义一个整数数组,求出其中奇数和偶数个数.

作者&投稿:陀购 (联系方式在网页底部的电邮联系)
java定义一个整数数组,求出其中的奇数和偶数个数。我是初学者,求代码和步骤解释,一定好评~

public class ArrTest {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10,12,14,13};
int m = 0;//记录偶数出现的次数
int n = 0;//记录奇数出现的次数
for(int i = 0; i < arr.length; i++){
if(arr[i]%2==0){
m++;
}else{
n++;
}
}
System.out.println("偶数出现的次数:"+m);
System.out.println("奇数出现的次数:"+n);
}
}
这个应该好懂吧

public class Demo {
public static void main (String args[]) {
int[] nums = {5, 2, 45, 11, 13, 32, 7, 24, 63, 48};
int odd=0, even=0;

for (int i=0; i<nums.length; i++) {
if (nums[i] % 2 == 0)
even ++;
else
odd ++;
}

System.out.println("even:" + even);
System.out.println("odd:" + odd);
}
}

public class Demo {
public static void main (String args[]) {
int[] nums = {5, 2, 45, 11, 13, 32, 7, 24, 63, 48};
int odd=0, even=0;

for (int i=0; i<nums.length; i++) {
if (nums[i] % 2 == 0)
even ++;
else
odd ++;
}

System.out.println("even:\t" + even);
System.out.println("odd:\t" + odd);
}
}

String a="1,2,3,4,5,6,7,8,9,10";
String[] b=a.split(",");
for(int i=0;i<b.length;i++){
float aaa=Float.parseFloat(b[i])/2;
//System.out.println(Float.parseFloat(b[i])/2);
String bbb=""+aaa;
String ccc=bbb.substring(bbb.indexOf(".")+1, bbb.indexOf(".")+2);
//System.out.println(bbb);
if(Integer.parseInt(ccc)>0){
System.out.println("质数:"+b[i]);
}else{
System.out.println("偶数:"+b[i]);
}
}

我写的比较 原始。

public class Java00 {
public static void main(String args[]) {
int cd = 0, cs = 0;
String s = "123214243535324324324234";
char[] c = s.toCharArray();
for (int i = 0; i < s.length(); i++) {
String str = c[i] + "";
int k = Integer.parseInt(str);
if (k % 2 == 0) {
cd++;
} else {
cs++;
}

}
System.out.println("偶数=" + cd + " ; 奇数=" + cs);
}
};

你直接就是一个判断语句,
用IF 语句进行判断就可以了啊。在数组里面采用循环的方式,在循环里面进行判断的,同时记录下奇数的个数和偶数的个数就可以了嘛。
例如
for (int i = 0 ; i < 数组长度-1;i++)
if (shuzu[i] % 2 )
{
jishugeshu ++;
}
if (shuzu[i] % 2 == 0 )
{
oushugeshu++;
}
然后再分别输出不就可以了 么。。

相关视频

JAVA定义一个整数数组,求出其中奇数和偶数个数.
public class Demo { public static void main (String args[]) { int[] nums = {5, 2, 45, 11, 13, 32, 7, 24, 63, 48};int odd=0, even=0;for (int i=0; i<nums.length; i++) { if (nums[i] % 2 == 0)even ++;else odd ++;} System.out.println("even:\\t" + even);System.out.println("odd:\\t" + odd);} }

java定义一个整数数组,求出其中的奇数和偶数个数。我是初学者,求代码和...
public class ArrTest { public static void main(String[] args) { int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10,12,14,13};int m = 0;\/\/记录偶数出现的次数 int n = 0;\/\/记录奇数出现的次数 for(int i = 0; i < arr.length; i++){ if(arr[i]%2==0){ m++;}...

java定义一个整数数组,含元素{2,6,9,12,8},获取并输出数组中数组角标和...
public class tmp { public static void main(String[] args){ int a[] = {2,6,9,12,8};int count = 0 ;for (int i = 0; i < a.length; i++){ if (i % 2 == 0 && a[i] % 2 == 0){ ++count;System.out.print(a[i] + " ");} } System.out.println("Total:...

java中int数组怎么定义
在Java中,定义int数组需明确数组名称和长度,初始化时通过索引赋值,访问时同样使用索引。具体如下:1. 定义int数组使用int[]声明数组类型,后跟数组名和new int[size]分配内存空间,其中size指定数组长度。语法:int[] arrayName = new int[size];示例:定义长度为5的数组numbersint[] numbers = new...

用java 编一个程序 定义一个n行n列的二维整数数组,赋初值,然后求出对角...
import java.util.Scanner;public class Matrix { public static void main(String[] args) { \/\/ TODO Auto-generated method stub System.out.println("请输入n*n数组,n=");Scanner sc = new Scanner(System.in);int n = sc.nextInt();int[][] a = new int[n][n];\/\/定义n*n数组 ...