Example use of array

Case: There are 1000 people who want to create statistics and the birth month of birth in most anything.
To get the month of birth randomly, Random class is used (for the real case, months of birth obtained from the database).

Note the following programs and results.
If not using an array, how the program?
sourceprint view?
01.import java.util.Random;
02.class StatistikBlnLahir (
03.public static void main (String [] args) (
04. String [] months = ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
05. "Okt", "Nov", "Dec");
06. jmlLahir int [] = new int [12];
07. Random bln = new Random ();
08. for (int i = 0; i <12; i + +) 09. jmlLahir [i] = 0; 10th / / declarations can also jmlLahir as below (no need for initialisation to) 11th / / int jmlLahir [] = (0,0,0,0,0,0,0,0,0,0,0,0); 12. int n = 0; 13. do ( 14. int blnLahir = bln.nextInt (12); 15. jmlLahir [blnLahir] = jmlLahir [blnLahir] +1; 16. n + +; 17. ) While (n <1000); 18. for (int i = 0; i <12; i + +) 19. System.out.println ( "month of birth" + months [i] + "" + jmlLahir [i] + "people"); 20. int max = 0; 21. for (int i = 0; i <12; i + +) 22. if (jmlLahir [i]> max)
23. JmlLahir = (max [i]; n = i;)
24. System.out.println ( "Most of birth month" + month [n] + "" + jmlLahir [n] + "people");
25.)
26.)

abbreviated name of month ... ... nulisnya lazy!
result

Born Jan 72 person months
Born Feb 93 persons
Born Mar 83 person months
Born Apr 99 person months
Born May 88 people
Born Jul 80 months
Born Jul 78 person months
Born Aug 86 person months
Born Sep 79 person months
Born Oct 74 person months
Born Nov 84 person months
Born Dec 84 person months
Most days of birth Apr 99 people

other example: Calculate the wages of the array and Sort Array
See explanation in the array http://java.lyracc.com/belajar/java-untuk-pemula/membuat-dan-menggunakan-array

0 komentar:

Post a Comment

+