Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package histogram;
  7.  
  8. import java.util.Arrays;
  9. import java.util.Scanner;
  10. import java.util.Random;
  11.  
  12. /**
  13. *
  14. * @author Conner
  15. */
  16. public class Histogram {
  17.  
  18. /**
  19. * @param args the command line arguments
  20. */
  21. public static void main(String[] args) {
  22. Scanner scan = new Scanner(System.in);
  23. System.out.println("Please enter the amount of number that you would"
  24. + " like to be placed in the histogram.");
  25. int no = scan.nextInt();
  26. int nomArry[] = getArray(no);
  27. System.out.println(Arrays.toString(nomArry));
  28. printHistogram(nomArry);
  29. }
  30.  
  31. public static int[] getArray(int no) {
  32. Random rand = new Random();
  33. int[] a = new int[no];
  34.  
  35. for (int i = 0; i < no; i++) {
  36. int random = rand.nextInt(100) + 1;
  37. a[i] = random;
  38. }
  39. return a;
  40. }
  41.  
  42. public static void printHistogram(int array[]) {
  43. int min = 1;
  44. int max = 10;
  45. String str;
  46. int numbers[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
  47. int stars[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  48. int length = array.length;
  49. int starLength = stars.length;
  50. for (int i = 0; i != length; i++) {
  51. for (int j = 0; j != starLength; j++) {
  52. if (array[i] > numbers[j] && array[i] <= numbers[j + 1]) {
  53. stars[j] += 1;
  54. }
  55. }
  56.  
  57. }
  58. int i = 0;
  59. while (min != 101 && max != 110) {
  60. str = (min + "-" + max + " ");
  61. System.out.print(str);
  62. for (int j = 1; j <= stars[i]; j++) {
  63. System.out.print("*");
  64. }
  65. min += 10;
  66. max += 10;
  67. i++;
  68. System.out.println();
  69.  
  70. }
  71.  
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement