【发布时间】:2015-07-19 00:53:22
【问题描述】:
我用以下代码创建了一个直方图:
import java.util.*;
public class Murray_A03Q3 {
public static void main (String [] args){
int num = 1;
int[] nums = new int[10];
List<Integer> list = new ArrayList<Integer>();
Scanner scan = new Scanner(System.in);
while(num != 0){
System.out.print("Enter a value to plot: ");
num = scan.nextInt();
System.out.println();
if(num != 0)
list.add(num);
}
for (int a = 0; a < list.size(); a++){
nums[(list.get(a)-1) / 10]++;
}
for (int count = 0; count < 10; count++){
System.out.print((count*1+1) + (count == 1 ? " ": " ") + "| \t");
for(int h = 0; h < nums[count]; h++)
System.out.print("#");
System.out.println();
}
} // end of main
} // end of class Murray
输出为:
1 | ######
2 |
3 |
4 |
5 |
6 | #
7 |
8 |
9 | #
10 |
但我需要它来打印来自 1-10 而不是 1-100 的用户输入的值,就像它似乎正在打印的那样(上面的输出使用了十个之外的两个值,这就是为什么一个在 6 和 9 处打印的原因) .我已经扫描了代码并尝试以不同的方式操纵它以实际获得我想要的东西,但我无法弄清楚。我的问题是,实际上需要更改什么才能获得 1-10 之间的值?
感谢您的帮助!
【问题讨论】: