Java implementation generates statistical print random numbers

Source: Internet
Author: User
Tags set set

Beginner Java, practice the array implementation Generate statistics print random number and set implementation generate statistics print random number

Array implementation generates statistical print random numbers

1 /**2 * Randomly generate 50 numbers (integers), the range of the first number is "10,50". Count the number of occurrences of each number and the number of times it occurs, and then print out each number and the number of occurrences, and do not print a number if it occurs 0 times. In ascending order of numbers when printing. */3 Importjava.util.Arrays;4  Public classrandomtest5 {6   Public Static voidMain (string[] args)7  {8   intTEM =-1;//used to set a two-dimensional array key variable that holds the same combined numeric and numeric occurrences9   int[] Array =New int[50];//generate an array that holds 50 random numbersTen   int[] statistic =New int[2] [50];//defines a two-dimensional array of 2 rows and 50 columns for setting the same combined numeric and numeric occurrences One   intMaxcount = 0;//maximum number of times A   //generates a random number between 10-50 and is stored in array arrays -    for(inti = 0; i < array.length;i++) -   { theArray[i] = (int) (Math.random () * 41) + 10;//Math.random returns a double value with a positive sign that is greater than or equal to 0.0 and less than 1.0.  -   } -   -   //Array Sorting + Arrays.sort (array); -  +   //count the number of occurrences of the same number and save the corresponding values and occurrences in a two-dimensional array statistic A    for(intval = 10;val < 50;val++) at   { -    intCount = 0; -     for(intx = 0; x < Array.Length; X + +) -    { -     if(Array[x] = =val) -     { inCount = count+1; -     } to    } +    if(Count! = 0) -    { theTEM = TEM +1; *Statistic[0][tem] =Val; $Statistic[1][tem] =count;Panax Notoginseng    } -   } the   //show sorted array values +    for(intj = 0;j < array.length;j++) A   { theSystem.out.print (Array[j] + ""); +   } - System.out.println (); $System.out.println ("-------------------------------"); $   //Show each number and number of occurrences and print out -    for(intA = 0; A < statistic[0].length;a++) -   { the    if(Statistic[0][a]! = 0) -    {WuyiSystem.out.println ("Value (" + statistic[0][a] + ") appeared altogether (" + statistic[1][a] + ") times! "); the    } -   } Wu   //list the number of occurrences and how many times it occurred -   //1. Find out the maximum number of occurrences About   // $   intIDX = 0; -    for(intCOT1 = 50; Cot1 > Maxcount; cot1--) -   { -     for(intb = 0; b < statistic[0].length; b++) A    { +     if(Statistic[1][b] = =cot1) the     { -Maxcount = COT1;//if STATISTIC[1][B] occurrences are equal to COT1, assign the value of COT1 to Maxcount so that the maximum number of occurrences is found.  $     } the    } the   } the   //find the maximum value and print the number of occurrences theSystem.out.println ("----------------------------------------------------------"); -System.out.println ("The most frequently occurring number is:"); in    for(inti = 0; i < statistic[0].length; i++) the   { the    if(Statistic[1][i] = =maxcount) About    { theSystem.out.print (Statistic[0][i] + "");//print out the most frequently occurring values the    } the   } + System.out.println (); -System.out.println ("Altogether out" + Maxcount + "Times! ");//Number of print occurrences the  Bayi  } the}

Collection implementation Generate statistics print random number

1 Importjava.util.ArrayList;2 Importjava.util.Collection;3 Importjava.util.Collections;4 ImportJava.util.Iterator;5 Importjava.util.List;6 ImportJava.util.Map;7 ImportJava.util.Random;8 ImportJava.util.Set;9 ImportJava.util.TreeMap;Ten  Public classrandomofcollection One { A  /** - * Randomly generate 50 numbers (integers), each with a range of [10,50]; counts the number of occurrences of each number and the number of occurrences of the most number; - * Finally, each number and its occurrences are printed in ascending order; the   * -   * @authorBreezedreams -   * @paramthe Ranval generated range is a random number of [10,50]; -   * @paramTreeMap Store random numbers and occurrences (key= random numbers; value= occurrences); +   * @paramlist stores the most frequently occurring random numbers; -   * @paramnumber random number occurrences; +   * @paramColl Gets the number of occurrences of statistics in TreeMap (collection type); A   * @paramMaxnum Gets the maximum number of occurrences in Coll; at   */ -   Public Static voidMain (string[] args) -  { -Random ran =NewRandom (); -Map TreeMap =NewTreeMap ();//store random numbers and occurrences (key= random numbers; value= occurrences); -List List =NewArrayList ();//the most frequently occurring random number; in   /** - * Randomly generates random numbers of 50 [10,50] and stores random numbers and occurrences in the TreeMap collection*/ toSystem.out.println ("Generated random numbers are:"); +    for(inti = 0; I < 50; i++) -   { theInteger Ranval =NewInteger (Ran.nextint (41) + 10); * System.out.println (ranval); $    if(Treemap.get (ranval) = =NULL)Panax Notoginseng    { -Treemap.put (Ranval,NewInteger (1)); the    } +    Else A    { the     intNumber = ((Integer) Treemap.get (Ranval)). Intvalue () + 1; +Treemap.put (Ranval,NewInteger (number)); -    } $   } $System.out.println ("-------------------------------------------------"); -Set set = Treemap.entryset ();//gets the Set view of the mapping relationship contained by the TreeMap (primarily for iterator traversal) -Collection coll = treemap.values ();//gets the number of occurrences; theInteger maxnum = (integer) Collections.max (coll);//get the maximum number of occurrences -   /**print random numbers and occurrences and increase the number of random occurrences to the list collection;*/Wuyi    for(Iterator iter =(Iterator) set.iterator (); Iter.hasnext ();) the   { -Map.entry Entry =(Map.entry) Iter.next (); WuInteger key = (integer) entry.getkey ();//get random number -Integer val = (integer) entry.getvalue ();//gets the number of occurrences AboutSystem.out.println ("Number:" + key + "appeared" + Val + "Times!) "); $    if(Val.intvalue () = =Maxnum.intvalue ()) -    { - List.add (key); -    } A   } +System.out.println ("---------------------------------------"); the   /**print the most frequently occurring random number and number of occurrences*/ -    for(Iterator iter =list.iterator (); Iter.hasnext ();) $   { theSYSTEM.OUT.PRINTLN ("Maximum number of occurrences:" +Iter.next ()); the   } theSystem.out.println ("altogether appeared" + Maxnum + "Times"); the  } -}

Java implementation generates statistical print random numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.