我给了这段代码我想编辑显示方法来产生像
这样的直方图0: *************
1: ******************
2: *************
最大的一个缩放到40个。
现在我不知道我是怎么想这样做的,我可以介绍一个for循环和一些打印方法,但我不认为这是我想要做的,输出没有'当我这样做时,看起来太整洁了。
public class LastDigitDistribution
{
private int[] counters;
/**
Constructs a distribution whose counters are set to zero.
*/
public LastDigitDistribution()
{
counters = new int[10];
}
/**
Processes values from this sequence.
@param seq the sequence from which to obtain the values
@param valuesToProcess the number of values to process
*/
public void process(Sequence seq, int valuesToProcess)
{
for (int i = 1; i <= valuesToProcess; i++)
{
int value = seq.next();
int lastDigit = value % 10;
counters[lastDigit]++;
}
}
/**
Displays the counter values of this distribution.
*/
public void display()
{
for (int i = 0; i < counters.length; i++)
{
System.out.println(i + ": " + counters[i]);
}
}
}
感谢所有帮助。 :)
答案 0 :(得分:0)
由于代码(数据源)和结构(显示调用)不完整,我有一个提示显示方法的问题:
for (int i = 0; i < counters.length; i++) {
System.out.print(i + ": " + counters[i] + " ");
for ( int j = 0 ; j < counters[i] ; j++ ) {
System.out.print( '*' );
}
System.out.println( "" ); // line feed only here
}
答案 1 :(得分:0)
我会这样:
for (int i = 0; i < counters.length; i++) {
System.out.println(StringUtils.repeat("*", counters[i]));
}