简体   繁体   English

我的生日问题代码没有打印任何东西

[英]My Birthday Problem code is not printing anything

I am an absolute beginner to learning programming and I was given this assignment:我是学习编程的绝对初学者,我被分配了这个任务:

Birthday problem.生日问题。 Suppose that people enter a room one at a time.假设人们一次进入一个房间。 How people must enter until two share a birthday?人们必须如何进入,直到两人生日相同? Counterintuitively, after 23 people enter the room, there is approximately a 50–50 chance that two share a birthday.与直觉相反,在 23 人进入房间后,两人生日相同的概率约为 50–50。 This phenomenon is known as the birthday problem or birthday paradox.这种现象被称为生日问题或生日悖论。 Write a program Birthday.java that takes two integer command-line arguments n and trials and performs the following experiment, trials times:编写一个程序 Birthday.java,它接受两个 integer 命令行 arguments n 并进行试验,并执行以下试验,试验次数:

  • Choose a birthday for the next person, uniformly at random between 0 and n−1.为下一个人选择生日,在 0 到 n-1 之间均匀随机。
  • Have that person enter the room.让那个人进入房间。
  • If that person shares a birthday with someone else in the room, stop;如果那个人和房间里的其他人同一天生日,停止; otherwise repeat.否则重复。

In each experiment, count the number of people that enter the room.在每个实验中,计算进入房间的人数。 Print a table that summarizes the results (the count i, the number of times that exactly i people enter the room, and the fraction of times that i or fewer people enter the room) for each possible value of i from 1 until the fraction reaches (or exceeds) 50%.打印一张表格,总结 i 从 1 到分数达到的每个可能值的结果(计数 i,恰好 i 人进入房间的次数,以及 i 或更少的人进入房间的次数的分数) (或超过)50%。

For more information on the assignment 有关任务的更多信息

However, my code won't print.但是,我的代码不会打印。 I would really appreciate if someone could help me find the problem to my assignment.如果有人能帮我找到我的作业中的问题,我将不胜感激。

public class Birthday {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]); //number of days
        int trials = Integer.parseInt(args[1]);
        boolean[] birthdays = new boolean[n];
        int[] times = new int[n + 2]; //number of times i people entered the room
        int r;

        for (int t = 1; t <= trials; t++) {

            for (int k = 0; k < n; k++) { //reset birthday
                birthdays[k] = false;
            }

            for (int i = 1; i <= n; i++) { //number of times

                r = (int) (Math.random() * (n - 1)); //random birthday

                if (birthdays[r] = false) {
                    birthdays[r] = true;
                    continue;
                }

                else if (birthdays[r] = true) {
                    times[i]++; //number of times i people entered the room + 1
                    break;
                }

            }

        }
        int j = 1;
        while ((double) times[j] / trials <= 0.5) {
            System.out.print(j + "\t" + times[j] + "\t" + ((double) times[j] / trials));
            j++;
            System.out.println("");

        }
    }
}

I can spot two errors from your code我可以从你的代码中发现两个错误

  1. As Scary Wombat pointed out, you are miss double equal sign inside of your if statement.正如 Scary Wombat 指出的那样,您在 if 语句中缺少双等号。

  2. The assignment is asking you to calculate "fraction of times that i or fewer people enter the room", meaning you need to do a summation for the first i indices and divided by trials.该作业要求您计算“我或更少人进入房间的次数”,这意味着您需要对前i个指数进行求和并除以试验。

For example, among 1 million trials, the fraction in which first duplicate birthday happens when 4th person enters is例如,在 100 万次试验中,当第 4 个人进入时第一个重复生日发生的分数是

(times[0] + times[1] + times[2] + times[3])/ 1000000

Here is what I got:这是我得到的:

1       0       0.0
2       2810    0.00281
3       5428    0.008238
4       8175    0.016413

As you can see the fraction is calculated by adding the first three elements together and then divided by 1000000 (2810 + 5428 + 8175 = 16413) / 1000000 = 0.016413如您所见,分数的计算方法是将前三个元素加在一起,然后除以 1000000 (2810 + 5428 + 8175 = 16413) / 1000000 = 0.016413

The way you are calculating the fraction ((double) times[j] / trials) is not correct.您计算分数((double) times[j] / trials)的方式不正确。

You are not adding the previous counts as shown in the example.您没有添加示例中所示的先前计数。 To do so, you can create a new variable to store the sums of previous counts.为此,您可以创建一个新变量来存储先前计数的总和。 and use it as your while loop condition.并将其用作您的 while 循环条件。 For instance, see below..例如,见下文..

csum += times[j]; // this adds the previous counts into a cumulative sum.

This cumulative sum is supposed to be the one u use to divide by trials to get your probability.这个累计总和应该是你用来除以试验以获得概率的总和。 Cheers!干杯!

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

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