標籤:
初學JAVA,練習一下數組實現產生統計列印隨機數和集合實現產生統計列印隨機數
數組實現產生統計列印隨機數
1 /** 2 * 隨機產生50個數字(整數),第個數位範圍是【10,50】。統計每個數字出現的次數以及出現次數最多的數字與它的個數,最後將每個數字及其出現次數列印出來,如果某個數字出現次數為0,則不要列印它。列印時按照數位升序排列。*/ 3 import java.util.Arrays; 4 public class RandomTest 5 { 6 public static void main(String[] args) 7 { 8 int tem = -1;//用於設定存放相同合并數值和數值出現次數的二維數組key變數 9 int[] array = new int[50];//產生存放50個隨機數的數組10 int[][] statistic = new int[2][50];//定義2行50列的二維數組,用於設定存放相同合并數值和數值出現次數11 int maxcount = 0;//最大次數12 //產生10-50之間的隨機數,並存放於array數組中13 for( int i = 0 ;i < array.length;i++)14 {15 array[i] = (int)(Math.random() * 41) + 10;//Math.random 返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0。16 }17 18 //array數組排序19 Arrays.sort(array);20 21 //統計相同數值一共出現的次數並將相應的數值和出現次數存於二維數組statistic中22 for(int val = 10;val < 50;val++)23 {24 int count = 0;25 for(int x = 0; x < array.length; x++)26 {27 if(array[x] == val)28 {29 count = count+1;30 }31 }32 if(count != 0)33 {34 tem = tem +1;35 statistic[0][tem] = val;36 statistic[1][tem] = count;37 }38 }39 //顯示排序後數組數值40 for(int j = 0;j < array.length;j++)41 {42 System.out.print(array[j] + " ");43 }44 System.out.println();45 System.out.println("-------------------------------");46 //顯示每個數值和出現的次數並列印出來47 for(int a = 0; a < statistic[0].length;a++)48 {49 if(statistic[0][a] != 0)50 {51 System.out.println("數值(" + statistic[0][a] + ")一共出現了(" + statistic[1][a] + ")次!");52 }53 }54 //列示出現次數最多的數以及出現了多少次55 //1、找出最大出現次數56 //57 int idx = 0;58 for(int cot1 = 50 ; cot1 > maxcount ; cot1--)59 {60 for(int b = 0; b < statistic[0].length; b++)61 {62 if(statistic[1][b] == cot1)63 {64 maxcount = cot1;//如果statistic[1][b]出現次數等於cot1,將cot1的值賦給maxcount,這樣就找出出現次數最大的次數。65 }66 }67 }68 //找出最大的數值並列印出現次數69 System.out.println("----------------------------------------------------------");70 System.out.println("出現次數最多的數值為:");71 for(int i = 0; i < statistic[0].length; i++)72 {73 if(statistic[1][i] == maxcount)74 {75 System.out.print(statistic[0][i] + " ");//列印出出現次數最多的數值76 }77 }78 System.out.println();79 System.out.println("一共出來了 " + maxcount + " 次!");//列印出現次數80 81 }82 }
集合實現產生統計列印隨機數
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Collections; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Random; 8 import java.util.Set; 9 import java.util.TreeMap;10 public class RandomOfCollection11 {12 /**13 * 隨機產生50個數字(整數),每個數位範圍是[10,50]; 統計每個數字出現的次數以及出現次數最多的數字與它的個數;14 * 最後將每個數字及其出現次數按照升序方式列印出來;15 *16 * @author Breezedreams17 * @param ranVal 產生範圍是[10,50]的隨機數;18 * @param treeMap 存放隨機數和出現次數(key=隨機數;value=出現次數);19 * @param list 存放出現次數最多的隨機數;20 * @param number 隨機數出現次數;21 * @param coll 擷取treeMap中統計的出現次數(Collection類型);22 * @param maxNum 擷取coll中的最大出現次數;23 */24 public static void main(String[] args)25 {26 Random ran = new Random();27 Map treeMap = new TreeMap();//存放隨機數和出現次數(key=隨機數;value=出現次數);28 List list = new ArrayList();//存放出現次數最多的隨機數;29 /**30 * 隨機產生50個[10,50]的隨機數並將隨機數和出現次數存放於treeMap集合中*/31 System.out.println("產生的隨機數分別為:");32 for (int i = 0; i < 50; i++)33 {34 Integer ranVal = new Integer(ran.nextInt(41) + 10);35 System.out.println(ranVal);36 if (treeMap.get(ranVal) == null)37 {38 treeMap.put(ranVal, new Integer(1));39 }40 else41 {42 int number = ((Integer) treeMap.get(ranVal)).intValue() + 1;43 treeMap.put(ranVal, new Integer(number));44 }45 }46 System.out.println("-------------------------------------------------");47 Set set = treeMap.entrySet();//擷取treeMap包含的映射關係的 Set 視圖(主要用於Iterator遍曆)48 Collection coll = treeMap.values();//擷取出現次數;49 Integer maxNum = (Integer)Collections.max(coll);//擷取最大出現次數50 /**列印隨機數和出現次數並將出現次數最多的隨機數增加到list集合當中;*/51 for (Iterator iter = (Iterator) set.iterator(); iter.hasNext();)52 {53 Map.Entry entry = (Map.Entry) iter.next();54 Integer key = (Integer) entry.getKey();//擷取隨機數55 Integer val = (Integer) entry.getValue();//擷取出現次數56 System.out.println("數字:" + key + "出現了 " + val + " 次!");57 if(val.intValue() == maxNum.intValue())58 {59 list.add(key);60 }61 }62 System.out.println("---------------------------------------");63 /**列印出現次數最多的隨機數以及出現次數*/64 for(Iterator iter = list.iterator(); iter.hasNext();)65 {66 System.out.println("出現次數最多的數值為:" + iter.next() );67 }68 System.out.println(" 一共出現了 " + maxNum + "次");69 }70 }
Java實現產生統計列印隨機數