统计一定范围内的数字出现次数

    技术2022-05-19  20

    import java.util.Random;

    public class HomeWork{

     

    public static void randomNumber(){

    int[] count = new int[41];

    Random random = new Random();

    for(int i = 0; i < 50; i++){

                        

          int number = random.nextInt(41) + 10;

                  count[number-10]++;       

    }

    for(int i = 0; i < count.length; i++){

    if(count[i] == 0){

    continue;

    }

    System.out.println((i + 10) + "出现的次数" + count[i] );

    }

     

    System.out.println("------------------------------------------------");

     

    int temp = 0;

    for(int i = 0; i < count.length; i++){

    if(temp < count[i])

    temp = count[i];

    }

     

    System.out.println("最大出现次数为:" + temp);

     

    for(int i = 0; i < count.length; i++){

    if(temp == count[i])

    System.out.print(i + 10 + " ");

    }

    }

     

    public static void main(String[] args){

    randomNumber();

     

    }

    }


    最新回复(0)