/*
有30个0-9之间的数字,分别统计0-9这十个数字分别出现了多少次
*/
class ExtracurricularWork1 {
public static void main(String[] args) {
//System.out.println("Hello World!");
int[] arr ={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 num0 = isEqual(arr,0);
int num1 = isEqual(arr,1);
int num2 = isEqual(arr,2);
int num3 = isEqual(arr,3);
int num4 = isEqual(arr,4);
int num5 = isEqual(arr,5);
int num6 = isEqual(arr,6);
int num7 = isEqual(arr,7);
int num8 = isEqual(arr,8);
int num9 = isEqual(arr,9);
printNum(0,num0);
printNum(1,num1);
printNum(2,num2);
printNum(3,num3);
printNum(4,num4);
printNum(5,num5);
printNum(6,num6);
printNum(7,num7);
printNum(8,num8);
printNum(9,num9);
}
/*
比较数组中的元素与传入数值是否相同,并返回次数
返回值类型: int
参数列表: int[] arr, int a;
*/
public static int isEqual(int[] arr,int a) {
int num = 0;
for (int i = 0;i < arr.length ;i++ ){
if (a == arr[i]){
num++;
}
}
return num;
}
/*
输出语句
返回值类型:void
参数列表: int a,int num;
*/
public static void printNum(int a,int num) {
System.out.println(a + "出现了" + num +"次");
}
}
|
|