如何从 Java 中的数组生成直方图输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15403987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:28:16  来源:igfitidea点击:

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

javaarraysmethodshistogram

提问by MaryK

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!!

所以我有一个方法可以调用我的 main,它接受一个包含 10 个数字的数组,并在嵌套的 for 循环中创建一个直方图输出。我不知道如何在行标题旁边获得正确数量的星号。数组从前一个方法传递给方法。谢谢!!

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();   
    }
}

回答by SudoRahul

If you plan to use your list, then you can probably have a forloop which looks like this:-

如果你打算使用你的list,那么你可能有一个for看起来像这样的循环:-

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

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

and the second forloop should be like this:-

第二个for循环应该是这样的:-

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

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

回答by Chris Cooper

Your test in the second for loop needs to be j < list[i]

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

回答by ghdalum

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:

您甚至不在 outputHistorgram 方法中的任何地方使用您的列表。我不知道这到底是不是应该产生,但也许像这样执行 for 循环可能会有所帮助:

for(int i : list)

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

如果这不正确,则给出示例输入和输出。

回答by vishal bhuva

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();   
}

}

}

回答by Riyar Ajay

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);
 }
 }