Java 作业 二

时间:2021-03-27 14:56:56

编写一个Java应用程序,统计数组{1,3,4,7,2,1,1,5,2,5,7,2,1,1,3},统计显示每种数字其出现的次数以及出现最多和最少次数的数字。

 package hi;

 public class Array2 {

     /**
* @param args
*/
private int [] arr1={1,3,4,7,2,1,1,5,2,5,7,2,1,1,3};
private int [] arr2={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//数组中的数字 对应依次出现的次数。
private int max,min,num1,num2;
Array2(){
max=0;
min=0;
num1=0;
num2=0;
}
public void print1(){
System.out.println("数字 次数"); }
// public void print2(){
// Array2 a = new Array2();
//
// }
public void record(){
Array2 a = new Array2();
for(int i=0;i<a.arr1.length;i++){
for(int j=0;j<a.arr1.length;j++){
if(a.arr1[i]==a.arr1[j]){
a.arr2[i]++;
}
}
}
a.min=a.arr2[0]; for(int i=0;i<a.arr1.length;i++){
if(a.max<a.arr2[i]){
a.max=a.arr2[i];
a.num1=i;
} if(a.min>a.arr2[i]){
a.min=a.arr2[i];
a.num2=i;
}
}
for(int i=0;i<a.arr1.length;i++){
System.out.print(a.arr1[i]+" ");
System.out.println(+a.arr2[i]);
}
System.out.println("The largest number is "+a.arr1[a.num1]);
System.out.println("The smallest number is "+a.arr1[a.num2]);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Array2 a = new Array2();
a.print1();
a.record(); } }