简体   繁体   中英

how do I generate histogram output from an array in Java?

so I have a method that I call to my main which takes an array of 10 numbers and creates a histogram output in a nested for loop. I can't figure out how to get the correct number of asterisks next to the row title. The array is passed to the method from a previous method. Thanks!!

public static void outputHistogram(int [] list)
{

    int k =0;
    for(int i=0;i<=9;++i)
    {
        System.out.print((i*10+1) +"-"+(i*10+10)+":"+"\t");

        for(int j=1; j<=i;++j)  
            System.out.print("*");


        System.out.println();   
    }
}

If you plan to use your "list" variable (which is actually an int array) for the histogram, then my recommendation for a double for loop would be as follows:

Outer loop (loop from 0 to the end of the list)

for (int i = 0; i < list.length; i++)

Inner loop (loop from 0 to the ith element of the outer loop)

for (int j = 0; j < list[i]; j++)

You don't even use your list anywhere in the outputHistorgram method. I don't know exactly was this should produce, but maybe doing the for-loop like this might help:

for(int i : list)

If this is not correct then give example input and output.

您在第二个 for 循环中的测试需要是j < list[i]

I think you want to do this.

public static void outputHistogram(int [] list)

{

int k =0;
for(int i=0;i<=9;++i)
{
    System.out.print((i*10+1) +"-"+(i*10+10)+":"+"\t");

    for(int j=1; j<=(i*10+10);++j)  
        System.out.print("*");


    System.out.println();   
}

}

import java.util.Scanner;
class Histogram
{
  private int count[]=new int[10];  // count array will keep elements of element
                                  // in particular range;
  public void showHistogram(int elements[])       // for example   27 15 34 22 11 11 19
  {                                 // in above input there is count[0]=0;
    for(int i=0;i<elements.length;i++)   // count[1]=4 and count[2]=2 and count[3]=1;
    {
     if(elements[i]>=0 && elements[i]<50)
     {
       if(elements[i]<10)
       {
          count[0]++;
       }
       else if(elements[i]>=10 && elements[i]<20)
       {
           count[1]++;
       }
       else if(elements[i]>=20 && elements[i]<30)
       {
           count[2]++;
       }
       else if(elements[i]>=30 && elements[i]<40)
       {
           count[3]++;
       }
       else
       {
          count[4]++;
       }
     }
     else if(elements[i]>=50 &&elements[i]<=100)
     {
       if(elements[i]<60)
       {
          count[5]++;
       }
       else if(elements[i]>=60 && elements[i]<70)
       {
           count[6]++;
       }
       else if(elements[i]>=70 && elements[i]<80)
       {
           count[7]++;
       }
       else if(elements[i]>=80 && elements[i]<90)
       {
           count[8]++;
       }
       else
       {
           count[9]++;
       }
     }
   }
   showHistogram1();
}
  private void showHistogram1()
  {
    System.out.println("Histogram of the elements:");
    for(int i=0;i<count.length;i++)     // this loop will print line
    {
       for(int j=0;j<count[i];j++)      // this will print elements element(*)
       {                                // at each line.
           System.out.print("* ");
       }
       if(count[i]!=0)                  // if line does'nt contain zero   
       System.out.println("");          // then if will change the row;
    }
  }
}
/*
  in above code if count[i]=zero means if there is elements
  element in particular range say [0-9] then it will
  elementst jump on next line;
*/
class HistogramGenerator   //Question3
{
public static void main(String args[])
{
     Histogram hg=new Histogram();
     System.out.println("Enter the elements of Elements want in a Histogram:");
     Scanner sc=new Scanner(System.in);

     int noOfElements=sc.nextInt();
     int histogramElements[]=new int[noOfElements];

     System.out.println("Enter the Elements for Histogram:");
     for(int i=0;i<noOfElements;i++)
     {
         histogramElements[i]=sc.nextInt();
     }

     hg.showHistogram(histogramElements);
 }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM