Anda di halaman 1dari 2

Note: Text content in the code blocks is automatically word-wrapped

1. import java.util.Random; 2. public class Histogram 3. { 4. 5. /*This is a program to generate random number histogram between 6. 1 and 100 and generate a table */ 7. 8. public static void main(String args[]) 9. { 10. 11. int [] randarray = new int [80]; 12. Random random = new Random(); 13. 14. 15. int i ; 16. for ( i = 0; i<randarray.length;i++) 17. { 18. int temp = random.nextInt(100); //random numbers up to number value 100 19. randarray[i] = temp; 20. 21. } 22. 23. int [] histo = new int [10]; 24. for (int j = 0; i<randarray.length; i++) 25. { 26. if (randarray[i] <= 10) { 27. // The value is less than 10... 28. // You want to increase the number of 0..10s 29. // Therefore, increase the first element of histo (not the ith element) 30. histo[0] = histo[0] + 1; 31. 32. } 33. else if ( randarray[i] <= 20){ 34. // Increase the second... 35. histo[1] = histo[1] + 1; 36. } 37. else if (randarray[i] <= 30){ 38. // An alternative way to increase a variable 39. histo[2] += 1; 40. } 41. else if ( randarray[i] <= 40){ 42. // Another alternative way to increase a variable 43. histo[3]++; 44. } 45. else if ( randarray[i] <= 50){ 46. // Another alternative way to increase a variable 47. histo[4]++; 48. } 49. else if ( randarray[i] <= 60){ 50. // Another alternative way to increase a variable 51. histo[5]++; 52. } 53. else if ( randarray[i] <= 70){ 54. // Another alternative way to increase a variable 55. histo[6]++; 56. } 57. else if ( randarray[i] <= 80){ 58. // Another alternative way to increase a variable 59. histo[7]++;

60. } 61. else if ( randarray[i] <= 90){ 62. // Another alternative way to increase a variable 63. histo[8]++; 64. } 65. else if ( randarray[i] <= 100){ 66. // Another alternative way to increase a variable 67. histo[9]++; 68. } 69. } 70. switch (randarray [i]) 71. { 72. case 1: System.out.print("0-10 | "); break; 73. case 2: System.out.print("11-20 | "); break; 74. case 3: System.out.print("21-30 | "); break; 75. case 4: System.out.print("31-40 | "); break; 76. case 5: System.out.print("41-50 | "); break; 77. case 6: System.out.print("51-60 | "); break; 78. case 7: System.out.print("61-70 | "); break; 79. case 8: System.out.print("71-80 | "); break; 80. case 9: System.out.print("81-90 | "); break; 81. case 10: System.out.print("91-100 | "); 82. } 83. 84. } 85. 86. }

Anda mungkin juga menyukai