Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package histogram;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.Random;
- /**
- *
- * @author Conner
- */
- public class Histogram {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Please enter the amount of number that you would"
- + " like to be placed in the histogram.");
- int no = scan.nextInt();
- int nomArry[] = getArray(no);
- System.out.println(Arrays.toString(nomArry));
- printHistogram(nomArry);
- }
- public static int[] getArray(int no) {
- Random rand = new Random();
- int[] a = new int[no];
- for (int i = 0; i < no; i++) {
- int random = rand.nextInt(100) + 1;
- a[i] = random;
- }
- return a;
- }
- public static void printHistogram(int array[]) {
- int min = 1;
- int max = 10;
- String str;
- int numbers[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
- int stars[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int length = array.length;
- int starLength = stars.length;
- for (int i = 0; i != length; i++) {
- for (int j = 0; j != starLength; j++) {
- if (array[i] > numbers[j] && array[i] <= numbers[j + 1]) {
- stars[j] += 1;
- }
- }
- }
- int i = 0;
- while (min != 101 && max != 110) {
- str = (min + "-" + max + " ");
- System.out.print(str);
- for (int j = 1; j <= stars[i]; j++) {
- System.out.print("*");
- }
- min += 10;
- max += 10;
- i++;
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement