如何读入数字 1-6 的列表并计算重复数字 java

How to read in a list of numbers 1-6 and count the repeated numbers java

所以我试图从一个文件中读入,该文件中有超过 10000000 个数字,范围为 1-6。本质上,这个练习的重点是模拟掷骰子,我需要找出每个数字被掷出的概率。我编写代码将随机数 1-6 写入文件,该文件为:

public static void main(String[]args)throws IOException{
    Scanner kb = new Scanner(System.in);
    PrintWriter pw = new PrintWriter(new File("Rolls.txt"));
    System.out.println("Input a number for the amount of rolls");

    long rolls = kb.nextLong();

    for(int i = 0; i< rolls; i++){
        
        if(i % 20 == 0){
            pw.println();
        }
        else{
        pw.print(((int)(Math.random()*6)+1) + ",");
        }
    }


   
    pw.close();
}

所以使用这个文件我想我应该将整行作为一个字符串读取,然后在逗号处拆分该行,然后将字符串更改为 longs/ints。我正在苦苦挣扎的是我应该计算 1 的数量 .... 6 并将该数字存储到另一个数组中,然后获取该数组的每个元素并找到它滚动的概率。我到目前为止为第二部分编写的代码如下。我尝试了一些方法,但没有任何效果,我很困惑。

public static void main(String[]args) throws IOException{
    Scanner in = new Scanner(new File("Rolls.txt"));
    long[] list = {0,0,0,0,0,0};
   
    while ( in.hasNextLine()){
        String line = in.nextLine();
        String[] numbers = line.split(",");
       
       
    }

感谢任何帮助。谢谢!

因为你输入的都是整数,你可能会用它们作为键

public static void main(String[]args) throws IOException {
    Scanner in = new Scanner(new File("Rolls.txt"));
    long[] list = {0,0,0,0,0,0};
    int parsed;

    while ( in.hasNextLine()){
        String line = in.nextLine();
        String[] numbers = line.split(",");

        for (String number : numbers) {
            parsed = Integer.parseInt(number);
            list[parsed - 1]++;
        }
    }

**我还没有测试过这个,但它应该可以工作

您用于生成角色的代码效果不佳。它在文件的开头放置了一个空的新行,因为在循环的第一次迭代中 i 等于 0。第二个问题是生成的卷的数量少于你想要的数量,因为在某些迭代中你只写了一个新行而没有数字。这就是我修改您的代码以正确生成卷的方式。

public static void main(String[]args)throws IOException{
    Scanner kb = new Scanner(System.in);
    PrintWriter pw = new PrintWriter(new File("Rolls.txt"));
    System.out.println("Input a number for the amount of rolls");

    long rolls = kb.nextLong();

    for(int i = 1; i<= rolls; i++){

        if(i % 20 == 0){
            pw.println();
        }
        pw.print(((int)(Math.random()*6)+1) + ",");
    }



    pw.close();
}

下面是您读取和计算概率的代码的延续。正如某些人已经评论过的那样,您应该使用 Integer.parseInt();将字符串值作为整数获取。

        public static void main(String[]args) throws IOException{
                Scanner in = new Scanner(new File("Rolls.txt"));
                long[] list = {0,0,0,0,0,0};
                while ( in.hasNextLine()){
                        String line = in.nextLine();
                        String[] numbers = line.split(",");
                        //parse the string to ints
                        for(int i=0;i<numbers.length;i++){
                                int currentValue = Integer.parseInt(numbers[i]);
                                list[currentValue-1]++;
                        }
                }
                long totalRollCount=0;
                for(int i=0;i<list.length;i++){
                        totalRollCount+=list[i];
                }
                System.out.println(totalRollCount);
                for(int i=0;i<list.length;i++){
                        System.out.println((i+1)+" "+((double)list[i]/totalRollCount));
                }
    }

Kamen Vakavchiev 和 Alexandre Hassan 的回答很好,但我想支持另一种方法使用 Java 8 Stream API 使代码更简单易读。

BufferedReader reader = new BufferedReader(new FileReader("Rolls.txt"));
Map<String, Long> result = reader.lines()
    .flatMap((Function<String, Stream<String>>) s -> Arrays.stream(s.split(",")))
    .collect(Collectors.groupingBy(s -> s, Collectors.summingLong(value -> 1)));
System.out.println(result);

可能的输出是:

{1=166381, 2=166643, 3=167300, 4=166644, 5=166197, 6=166835}