简体   繁体   English

为什么我的JAVA代码中出现此错误?

[英]Why am I getting this error in my JAVA code?

The birthday paradox says that the probability that two people in a room will have the same birthday is more than half as long as the number of people in the room (n), is more than 23. This property is not really a paradox, but many people find it surprising. 生日悖论说,一个房间中的两个人有相同的生日的概率是该房间中的人数(n)的一半以上,即大于23。此属性并不是真正的悖论,而是许多人发现这令人惊讶。 Design a C++ program that can test this paradox by a series of experiments on randomly generated birthdays, which test this paradox for n =5, 10, 15, 20, . 设计一个C ++程序,该程序可以通过在随机生成的生日上进行一系列实验来测试该悖论,该实验针对n = 5、10、15、20,...测试该悖论。 . . , 100. You should run at least 10 experiments for each value of n and it should output, for each n, the number of experiments for that n, such that two people in that test have the same birthday. ,即100。您应该为n的每个值至少运行10个实验,并且应该为每个n输出该n的实验数量,以使该测试中的两个人有相同的生日。

package birth;
import java.util.Random;

/* Question: 
The birthday paradox says that the probability that two people in a room
will have the same birthday is more than half as long as the number of
people in the room (n), is more than 23. This property is not really a paradox,
but many people find it surprising. Design a C++ program that can
test this paradox by a series of experiments on randomly generated birthdays,
which test this paradox for n =5, 10, 15, 20, . . . , 100. You should run
at least 10 experiments for each value of n and it should output, for each
n, the number of experiments for that n, such that two people in that test
have the same birthday.
*/

public class birth {

public static final int YEAR = 365;

public static void main(String[] args)
{

    int numOfPeople = 5;
    int people = 5;

    //DOB array
    int[] birthday = new int[YEAR];



    //Creates an array that represents 365 days
    for (int i = 0; i < birthday.length; i++)
        birthday[i] = i + 1;

    //Random Number generator
    Random randNum = new Random();

    int iteration = 1;

    //iterates around peopleBirthday array
    while (numOfPeople <= 100)
    {
        System.out.println("Iteration: " + iteration);
        System.out.println();
        //Creates array to holds peoples birthday
        int[] peopleBirthday = new int[numOfPeople];


        //Assigns people DOB to people in the room
        for (int i = 0; i < peopleBirthday.length; i++)
        {
            int day = randNum.nextInt(YEAR + 1);
            peopleBirthday[i] = birthday[day];


        }
        for (int i = 0; i < peopleBirthday.length; i++)
        {   


            //stores value for element before and after
            int person1 = peopleBirthday[i];
            int person2 = i + 1;

            //Checks if people have same birthday
            for (int j = person2; j < peopleBirthday.length; j++)
            {


                //Prints matching Birthday days
                if (person1 == peopleBirthday[j])
                {
                    System.out.println("P1: " + person1 + " P2: " + peopleBirthday[j]);
                    System.out.println("Match!!! \n");

                }
            }
        }


        //Increments the number of people in the room
        numOfPeople += 5;
        iteration++;
    }

    }
}

I am getting a error: java.lang.ArrayIndexOutOfBoundsException: 365 I can't figure out what is wrong with my code 我收到一个错误: java.lang.ArrayIndexOutOfBoundsException: 365我无法弄清楚我的代码出了什么问题

It would be nice if you provided the exact line number where the exception was thrown (the information is in the error stack trace that you got), but it's very likely that the problem occurs here: 如果您提供了引发异常的确切行号(信息在您得到的错误堆栈跟踪中),那将是很好的选择,但是很有可能在这里发生问题:

int day = randNum.nextInt(YEAR + 1); // 365 + 1 = 366
peopleBirthday[i] = birthday[day];

The documentation for Random.nextInt says: Random.nextInt的文档说:

Returns: the next pseudorandom, uniformly distributed int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence. 返回:下一个伪随机数,此随机数生成器的序列在0(含)和有界(不含)之间均匀分布的int值。

In this case, you are calling Random.nextInt with a value of 366 ( 365 + 1 ), so that means that you are effectively reading some random number between 0 and 365 . 在这种情况下,您将调用366365 + 1 )值的Random.nextInt ,这意味着您正在有效地读取0365之间的某个随机数。 If you ever do get 365 , that will make birthday[day] throw the out of bounds exception, as the maximum index of your array is 364 , not 365 . 如果您确实得到365 ,则将使birthday[day]超出范围,因为数组的最大索引为364而不是365

You probably meant to read the random value this way instead: 您可能打算用这种方式读取随机值:

int day = randNum.nextInt(YEAR); // 365 (exclusive)

Arrays in Java are zero based. Java中的数组从零开始。 If you create birthday with a length of 365 , the indexes would be from 0 to 364 . 如果您创建的birthday长度为365 ,则索引的范围为0364

You need to change this line from this: 您需要从以下更改此行:

int[] birthday = new int[YEAR];

To this: 对此:

int[] birthday = new int[YEAR+1];

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

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