时间:2022-12-11 11:54:05 | 栏目:JAVA代码 | 点击:次
随机产生50个10到50的整数,统计每个数字各出现几次,出现0次的数字不打印。
代码如下:
package com.homework.lhh; import java.util.Random; public class Ex04 { public static void main(String[] args) { int[] array = new int[50]; Random random = new Random(); for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(41) + 10; System.out.print(array[i] + " "); } for (int i = 0; i < array.length - 1; i++) { for (int j = 0; j < array.length - i - 1; j++) { if (array[j] > array[j + 1]) { int tmp = array[j]; array[j] = array[j + 1]; array[j + 1] = tmp; } } } System.out.println(); //为了方便直观的统计 System.out.println("冒泡排序后的数组为:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } /* * for (int i = 0; i < array.length; i++) { if(0 == array[i]){ continue; * } if(array[i] == array[i+1]){ conut++; } System.out.println(); * System.out.print( array[i] + "出现的次数为:"+ (conut+1) +" 次"); conut = 0; * } */ // 查找 System.out.println(); for (int i = 0; i < array.length; i++) { int conut = 0;// 定义每一个数出现的次数 for (int j = 0; j < array.length; j++) { if (array[i] == array[j]) { conut++; } } if (i < 49 && array[i] != array[i + 1] && conut != 0) { System.out.println(array[i] + "出现的次数为:" + conut + " 次"); } if(i == 49){ System.out.println(array[i] + "出现的次数为:" + conut + " 次"); } } } }
运行结果如下: