好吧所以我制作了一个程序,打印出数字0-9被输入的次数,它应该永远持续下去,直到用户键入一个负数,然后它将停止程序并打印出结果,到目前为止,我的代码是:
import java.util.Scanner;
public class opg33 {
public static void main(String args[]) {
System.out.println(1 + ":" + "\t" + "Integer 0-9");
Scanner keyboard = new Scanner(System.in);
int[] a = new int[10];
int counter = 0;
int input;
input = keyboard.nextInt();
boolean exit = true;
while (exit == true) {
counter++;
System.out.println((counter + 1) + ":" + "\t" + "Integer 0-9");
if (input > -1 && input < 10) {
a[input]++;
}
input = keyboard.nextInt();
if (input < 0) {
exit = false;
for (int i = 0; i < a.length; i++) {
System.out.print(i + ":");
for (int n = 0; n < a[i]; n++)
System.out.print("x");
System.out.println();
}
}
}
}
}
你可以看到这个程序完全正常,就像我想要的那样!我唯一的问题是,如何使其像条形图(列)一样垂直打印,而不是水平打印?提前谢谢!
答案 0 :(得分:2)
你的意思是......?
1: Integer 0-9
1
2: Integer 0-9
1
3: Integer 0-9
2
4: Integer 0-9
2
5: Integer 0-9
2
6: Integer 0-9
3
7: Integer 0-9
3
8: Integer 0-9
7
9: Integer 0-9
8
10: Integer 0-9
-1
x
x x x
x x x x x
0 1 2 3 4 5 6 7 8 9
您需要做的第一件事是确定图表的最大高度......
// Calculate the required height...
/*
All this loop does, is loops through the array "a"
and finds the maximum value contained within,
this is the largest number of times the user
entered that value
The loop itself is just an enhanced "for-next-loop",
that is, it's a short cutted way of saying...
for (int index = 0; index < a.length; index++) {
int value = a[index];
...
}
*/
int maxFrequency = 0;
for (int value : a) {
// This is a really simply way to calculate the max value within
// range, it just says, return me the value which is the larger
// of two...
maxFrequency = Math.max(value, maxFrequency);
}
接下来,您需要打印图表的每一行,从顶部开始并向底部移动。对于每一列,您需要确定它是否具有要在该行中显示的值...
// This builds each row
for (int row = maxFrequency; row > 0; row--) {
// This is used instead of String concatenation (String + String)
// This is the preferred mechanism for doing String concatenation
// in loops as it uses less memory and will generally be faster...
StringBuilder sb = new StringBuilder(30);
// We need to inspect each value in the array to determine
// if we need to display something...
// The basic idea is, we will only start displaying "x"
// when the value in the array >= the row number...
for (int value : a) {
// This just appends each column value as
// required, prefixing and suffixing it with spaces
sb.append(" ");
if (value >= row) {
sb.append("x");
} else {
sb.append(" ");
}
sb.append(" ");
}
// Print the row...
System.out.println(sb.toString());
}
最后,你需要打印页脚......
StringBuilder sb = new StringBuilder(30);
for (int i = 0; i < a.length; i++) {
sb.append(" ").
append(i).
append(" ");
}
System.out.println(sb.toString());
StringBuilder
可以写一本关于这个主题的书,所以这里提供的信息只是概述......
应该使用 StringBuilder
,尤其是在循环中连接String
时,主要是因为它减少了创建的临时,短期对象的数量,并使该过程更有效。
通常,Java编译器会在内部将String
并置转换为使用StringBuilder
,但由于循环的性质,不能这样做。在你的情况下,它可能不是那么重要,但是在长时间运行的程序中,它可能遍历循环,数百,数千甚至数百万次,它成为性能瓶颈,因此形成一个好习惯
在内部,StringBuilder
是一个自我管理的char
数组,这不是特别重要,但很高兴知道。当您使用.append
时,StringBuilder
会获取内容,在其内部缓冲区中为其腾出空间并将内容写入其中。这可能听起来像是在引入它自己的开销,并且在某种程度上它是(你可以做一些事情来控制这些),但它不如运行垃圾收集周期。 ..
toString
然后只需将char
数组转换为String
对象...