1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
* 求出上面数组中0-9分别出现的次数
*
* 2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
* 要求求出其中的奇数个数和偶数个数。
@Test
public void test1() {
/**
* 1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
* 求出上面数组中0-9分别出现的次数(5种方法)
*
* 2、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
* 要求求出其中的奇数个数和偶数个数。
*/
int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
int[] count=new int[10];
//第一种方法:
int num=0;
for (int i = 0; i <= 9; i++) {
for (int j = 0; j < scores.length; j++) {
if (scores[j]==i) {
num++;
}
}
System.out.println("数组中"+i+"出现的次数为:"+num);
num=0;
}
/*
//第二种方法:
for (int i : count) {
switch (i) {
case 0:
count[0]++;
break;
case 1:
count[1]++;
break;
case 2:
count[2]++;
break;
case 3:
count[3]++;
break;
case 4:
count[4]++;
break;
case 5:
count[5]++;
break;
case 6:
count[6]++;
break;
case 7:
count[7]++;
break;
case 8:
count[8]++;
break;
default:
count[9]++;
break;
}
}*/
/*
//第三种方法:
for (int i : scores) {
if (i==0) {
count[0]++;
}else if (i==1) {
count[1]++;
}else if (i==2) {
count[2]++;
}else if (i==3) {
count[3]++;
}else if (i==4) {
count[4]++;
}else if (i==5) {
count[5]++;
}else if (i==6) {
count[6]++;
}else if (i==7) {
count[7]++;
}else if (i==8) {
count[8]++;
}else if (i==9) {
count[9]++;
}
}*/
/*
//第四种方法:
for (int i = 0; i < scores.length; i++) {
count[scores[i]]++;
}*/
/*
//第五种方法:
for (int i = 0; i <=9; i++) {
System.out.println("数组中"+i+"出现的次数为:"+count[i]);
}*/
int oushu=0;
int jishu=0;
for (int i:scores) {
if (i%2==0) {
oushu++;
}else {
jishu++;
}
}
System.out.println("数组中奇数个数为:"+jishu+",偶数个数为:"+oushu);
}