简体   繁体   English

Java计数器问题

[英]Java counter issue

I am trying to write a program that reads values from a text file and checks how many values are within certain value ranges.我正在尝试编写一个程序,该程序从文本文件中读取值并检查特定值范围内有多少个值。 The point is to eventually have the program print to the console the total number of values for each range.关键是最终让程序将每个范围的值总数打印到控制台。 I have had it where it can do this 1 value at a time but then it would reset with every new int.我已经有了它一次可以执行 1 个值的地方,但是它会随着每个新的 int 值而重置。 In it's current it would simply repeat the first number over and over.在当前,它只会一遍又一遍地重复第一个数字。 To reiterate, I am trying to get it to report once it sorts through the values in the text file the amount of times a value was within the number ranges.重申一下,我试图让它在对文本文件中的值进行排序后报告值在数字范围内的次数。

Here is my code.这是我的代码。

package lab2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab2 {

    public static void main(String[] args) throws FileNotFoundException {

        File file = new File("lab2_input.txt");
        Scanner scan = new Scanner(file);

        int counter1 = 0;
        int counter2 = 0;
        int counter3 = 0;
        int counter4 = 0;
        int counter5 = 0;
        int counter6 = 0;
        int counter7 = 0;
        int counter8 = 0;

        while(scan.hasNextLine()) {

            int number = scan.nextInt();
            System.out.println(number);

            if (number <= 24){
                counter1++;
            }
            if (25 <= number && number <= 49){
                counter2++;
            }
            if (50 <= number && number <= 74){
                counter3++;
            }
            if (75 <= number && number <= 99){
                counter4++;
            }
            if (100 <= number && number <= 124){
                counter5++;
            }
            if (125 <= number && number <= 149){
                counter6++;
            }
            if (150 <= number && number <= 174){
                counter7++;
            }
            if (175<= number && number >= 200){
                counter8++;
            }

        }

        System.out.println(counter1);
        System.out.println(counter2);
        System.out.println(counter3);
        System.out.println(counter4);
        System.out.println(counter5);
        System.out.println(counter6);
        System.out.println(counter7);
        System.out.println(counter8);
    }

}

I think you mean to check for example range from 25 to 49, then instead of:我认为您的意思是检查例如范围从 25 到 49,而不是:

if (25 <= number && number <= 49){
    counter2++;
}

you should write:你应该写:

if (25 >= number && number <= 49){
    counter2++;
}

Same thing about other checks.其他检查也一样。

PS: I would suggest making counter an array and increment the elements based on calculated index, because the ranges are always 25: var index = number / 25 . PS:我建议将counter设为数组并根据计算的索引增加元素,因为范围始终为 25: var index = number / 25

That looks like a lot of manual counter keeping;这看起来像很多手动counter your desired intervals are regularly separated at twenty-five, so I would use an array.您所需的间隔定期间隔为 25,因此我将使用数组。 Additionally, use try-with-Resources to avoid leaking file handles.此外,使用try-with-Resources以避免泄漏文件句柄。 And use Scanner.hasNextInt() with Scanner.nextInt() (not Scanner.hasNextLine() ).并使用Scanner.hasNextInt()Scanner.nextInt() (不是Scanner.hasNextLine() )。 Finally, I would add more descriptive messaging around the counts and their ranges.最后,我将围绕计数及其范围添加更多描述性消息。 Like,喜欢,

File file = new File("lab2_input.txt");
int[] counters = new int[9];
try (Scanner scan = new Scanner(file)) {
    while (scan.hasNextInt()) {
        int v = scan.nextInt();
        if (v >= 0 && v <= 200) {
            counters[v / 25]++;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
for (int i = 0; i < counters.length; i++) {
    System.out.printf("The range %d to %d had %d values%n", i * 25, 
            ((i + 1) * 25) - 1, counters[i]);
}

You can use else-if blocks instead of multiple if blocks and get rid of the lower bound validation.您可以使用else-if块而不是多个if块并摆脱下限验证。 This would make some performance improvement as well if you are dealing with a large list of numbers.如果您正在处理大量数字,这也会带来一些性能改进。

    if (number <= 24){
        counter1++;
    } else if (number <= 49){
        counter2++;
    } else if (number <= 74){
        counter3++;
    } else if (number <= 99){
        counter4++;
    } else if (number <= 124){
        counter5++;
    } else if (number <= 149){
        counter6++;
    } else if (number <= 174){
        counter7++;
    } else if (number <= 200){
        counter8++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM