我的程序从文件中读取任意数量的整数,然后打印1到100之间所有整数的直方图条形图。
代码工作,我已经尝试使其缩短所有可能的格式,但然后我的代码停止工作。所以这个长版本是目前唯一可用的版本。
所以我的问题只是出于好奇,如果我的if语句和直方图的打印有一个较短的方式。
注意:并非文件中的所有整数都必须位于区间[1-100]
Create an object representing a file
File file = new File("path");
Scanner fileScan = new Scanner(file);
ArrayList<Integer> list = new ArrayList<Integer>();
int total=0;
while (fileScan.hasNext()){
total++;
list.add(fileScan.nextInt());
}
int [] counter = new int [10];
for(int i=0; i<list.size();i++){
if (list.get(i) >=1 && list.get(i)<=10){
counter[0]++;
}
if (list.get(i) >10 && list.get(i)<=20){
counter[1]++;
}
if (list.get(i) >20 && list.get(i)<=30){
counter[2]++;
}
if (list.get(i) >30 && list.get(i)<=40){
counter[3]++;
}
if (list.get(i) >40 && list.get(i)<=50){
counter[4]++;
}
if (list.get(i) >50 && list.get(i)<=60){
counter[5]++;
}
if (list.get(i) >60 && list.get(i)<=70){
counter[6]++;
}
if (list.get(i) >70 && list.get(i)<=80){
counter[7]++;
}
if (list.get(i) >80 && list.get(i)<=90){
counter[8]++;
}
if (list.get(i) >90 && list.get(i)<=100){
counter[9]++;
}
}
int valueTotal=0;
for (int j=0; j<counter.length; j++){
valueTotal += counter[j];
}
System.out.print("Reading integers from file: "+file);
System.out.print("\nNumber of integers in the interval [1,100]: "+valueTotal);
System.out.print("\nOthers: "+(total-valueTotal));
System.out.print("\nHistogram\n");
System.out.print("1 - 10 | ");
display(counter[0]);
System.out.print("\n11 - 20 | ");
display(counter[1]);
System.out.print("\n21 - 30 | ");
display(counter[2]);
System.out.print("\n31 - 40 | ");
display(counter[3]);
System.out.print("\n41 - 50 | ");
display(counter[4]);
System.out.print("\n51 - 60 | ");
display(counter[5]);
System.out.print("\n61 - 70 | ");
display(counter[6]);
System.out.print("\n71 - 80 | ");
display(counter[7]);
System.out.print("\n81 - 90 | ");
display(counter[8]);
System.out.print("\n91 - 100| ");
display(counter[9]);
}
public static void display(int n){
for (int i=0; i<n; i++){
System.out.print("*");
}
}
}
我的输出:
Reading integers from file: ....txt
Number of integers in the interval [1,100]: 18
Others: 4
Histogram
1 - 10 | ******
11 - 20 | *
21 - 30 | ***
31 - 40 |
41 - 50 | *
51 - 60 | *
61 - 70 | *
71 - 80 | ***
81 - 90 | *
91 - 100| *
答案 0 :(得分:1)
是的,counter[(list.get(i) - 1) / 10]++;
或类似的东西应该能够替换所有那些if语句。
编辑:根据您更改的要求,您需要首先测试索引是否超出范围:
int index = (list.get(i) - 1 ) / 10;
if (index >= 0 || index < counter.length) {
counter[index]++;
} else {
// not sure if you need to do something for index out of bounds
}
答案 1 :(得分:1)
在收集大小为10的段中的值时,可以使用/
(整数除法)来计算索引:
for (int i=0; i<list.size(); i++) {
int value = Math.min(list.get(i), 100);
int index = value / 10 - 1;
counter[index]++;
}
答案 2 :(得分:-2)
counter[(list.get(i) - 1) / 10]++;
似乎最简洁。您还可以在for循环中使用switch statement来删除ifs并将所有内容放在同一代码块中。此外,将元素存储在第i个索引中可以节省大量重复get
ting
编辑:原始代码与Java语法不一致。这是更新版本。 switch
中的表达式是关键:
for(int i = 0; i < lst.size(); i++){
int x = lst.get(i);
switch((x-1)/10){
case 1: counter[0]++;
case 2: counter[1]++;
case 3: counter[2]++;
case 4: counter[3]++;
case 5: counter[4]++;
case 6: counter[5]++;
case 7: counter[6]++;
case 8: counter[7]++;
case 9: counter[8]++;
}
}