我如何生成Java中的数组直方图输出? [英] how do I generate histogram output from an array in Java?
问题描述
所以我有,我打电话给我的主这需要10个数字组成的数组,并创建一个嵌套的直方图输出循环的方法。我无法弄清楚如何获得星号正确数量的旁边一行标题。该阵列被传递给从previous方法的方法。谢谢!!
公共静态无效outputHistogram(INT []列表)
{ INT K = 0;
的for(int i = 0; I< = 9; ++ I)
{
System.out.print((ⅰ* 10 + 1)+ - +(ⅰ* 10 + 10)+:+\\ t的); 对于(INT J = 1; J< = I; ++ j)条
System.out.print(*);
的System.out.println();
}
}
如果您打算使用列表
,那么你可能有一个为
循环看起来像这样: -
的for(int i = 0; I< List.length的数字,我++)
和第二为
循环应该是这样的: -
为(INT J = 0; J<名单[I]; J ++)
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
, then you can probably have a for
loop which looks like this:-
for(int i=0;i<list.length;i++)
and the second for
loop should be like this:-
for(int j=0; j<list[i];j++)
这篇关于我如何生成Java中的数组直方图输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!