Requirements:
50 numbers (integers) are randomly generated. The range of each number is [10, 50]. The number of occurrences of each number and the number of occurrences of the number are counted, finally, print each number and the number of occurrences. If the number of occurrences of a number is 0, do not print it. The numbers are displayed in ascending order.
Version 1: (implemented using arrays)
Import Java. util. random; public class homework {public static void main (string [] ARGs) {int [] Count = new int [50]; // count the number of times each number appears for (INT I = 0; I <50; I ++) {int number = new random (). nextint (41) + 10; // generate a random number between [10, 50] Count [number-10] + +; // take full advantage of the information provided by the array subscript, the subscript information is used to obtain a specific number, and the values in the array represent the number of occurrences of a specific number. In this way, an array meets the two requirements. For example, Count [0] and count [1] indicate the number of occurrences of 10 and 11 respectively, and so on ...} for (INT I = 0; I <count. length; I ++) {If (count [I]! = 0) {system. out. println (I + 10) + ":" + Count [I]) ;}} int max = count [0]; for (INT I = 0; I <count. length; I ++) // find the maximum number of times {If (count [I]> MAX) {max = count [I] ;}} for (INT I = 0; I <count. length; I ++) {If (count [I] = max) {system. out. println ("the maximum number of occurrences is:" + (I + 10) + ", and the number of occurrences is:" + Count [I]) ;}}} /* the output result in eclipse is: 12 appears: 113 appears: 314 appears: 315 appears: 116 appears: 317 appears: 120 appears: 122 appears: 423 appears: 324 appears: 427 appears: 128 appears: 129 appears: 230 appears: 231 appears: 232 appears: 133 appears: 134 appears: 137 appears: 343 appears: 246 appears: 248 appears: 349 appears: the number of occurrences of 150 is: the number of occurrences of 4 is: 22, and the number of occurrences of 4 is: 24. The number of occurrences of 4 is: 50, number of times: 4 */
Version 2: (using collections)
Import Java. util. arraylist; import Java. util. collection; import Java. util. collections; import Java. util. iterator; import Java. util. list; import Java. util. map; import Java. util. random; import Java. util. set; import Java. util. treemap; public class homeworkstd {public static void main (string [] ARGs) {map = new treemap (); // Stores numbers and Their Occurrences (key is a number, value is the number of occurrences) Try hashmap maprandom random = new random (); For (INT I = 0; I <50; I ++) {int number = random. nextint (41) + 10; integer in = new INTEGER (number); If (map. get (in) = NULL) {map. put (in, new INTEGER (1);} else {int value = (integer) map. get (in )). intvalue (); map. put (in, new INTEGER (Value + 1);} collection Col = map. values (); integer maxoccurs = (integer) collections. max (COL); // you can find the maximum number of times by yourself, but use the JDK as much as possible, because the efficiency may be higher than what you write. Set set = map. entryset (); List list = new arraylist (); // store the number for (iterator iter = set. iterator (); ITER. hasnext ();) {map. entry entry = (map. entry) ITER. next (); integer key = (integer) entry. getkey (); integer value = (integer) entry. getvalue (); If (value. intvalue () = maxoccurs. intvalue () {list. add (key);} system. out. println (Key + ":" + value);} system. out. println ("the maximum number of occurrences is" + maxoccurs); system. out. println ("these numbers are:"); For (iterator iter = List. iterator (); ITER. hasnext ();) {system. out. println (ITER. next ();}/* Another method // find the maximum number of times (self-implemented) Collection coll = map. values (); int max = 0; // assume that the maximum number of times is zero for (iterator iter = Coll. iterator (); ITER. hasnext ();) {integer value = (integer) ITER. next (); If (value. intvalue ()> MAX) max = value. intvalue ();} // set = map. keyset (); For (iterator iter = set. iterator (); ITER. hasnext ();) {INTEGER key = (integer) ITER. next (); If (integer) map. get (key )). intvalue () = max) {system. out. println ("the maximum number of occurrences is:" + key + ", and the number of occurrences is:" + max );}}*/}}
Version 3: (simplified version, using a set, using generic, automatic packing/unpacking Technology)
Import Java. util. collection; import Java. util. collections; import Java. util. iterator; import Java. util. map; import Java. util. random; import Java. util. set; import Java. util. treemap; public class statisticrandomnumber {public static void main (string [] ARGs) {treemap <integer, integer> map = new treemap <integer, integer> (); random random = new random (); int temp; For (INT I = 0; I <50; I ++) {temp = random. nextint (41) + 10; If (map. get (temp) = NULL) map. put (temp, 1); else map. put (temp, map. get (temp) + 1);} set <map. entry <integer, integer> mapset = map. entryset (); For (iterator <map. entry <integer, integer> iter = mapset. iterator (); ITER. hasnext ();) {map. entry <integer, integer> entry = ITER. next (); system. out. println ("key:" + entry. getkey () + ", value:" + entry. getvalue ();} collection <integer> Col = map. values (); int maxvalue = collections. max (COL); system. out. println ("Maximum number of occurrences:" + maxvalue); system. out. println ("these numbers are:"); For (iterator <map. entry <integer, integer> iter = mapset. iterator (); ITER. hasnext ();) {map. entry <integer, integer> entry = ITER. next (); If (entry. getvalue () = maxvalue) {system. out. println (entry. getkey ());}}}}
Random Number statistics