【问题标题】:Why am I getting this error in my JAVA code?为什么我的 JAVA 代码中出现此错误?
【发布时间】:2015-08-29 03:54:00
【问题描述】:

生日悖论说两个人在一个房间里的概率 将有相同生日的人数是人数的一半以上 房间里的人(n),是23多。这个属性真的不是悖论, 但很多人对此感到惊讶。设计一个 C++ 程序,它可以 通过对随机生成的生日进行一系列实验来检验这个悖论, 它测试了 n = 5, 10, 15, 20, 的这个悖论。 . . , 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++;
    }

    }
}

我收到一个错误:java.lang.ArrayIndexOutOfBoundsException: 365 我不知道我的代码有什么问题

【问题讨论】:

  • Design a C++ program... 但您的代码在Java。哎呀

标签: java arrays loops birthday-paradox


【解决方案1】:

如果您提供引发异常的确切行号会很好(信息在您获得的错误堆栈跟踪中),但问题很可能发生在这里:

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

Random.nextInt 的文档说:

返回:来自此随机数生成器序列的下一个伪随机、均匀分布的 int 值,介于零(包括)和边界(不包括)之间。

在这种情况下,您调用Random.nextInt 时的值为366 (365 + 1),这意味着您实际上是在读取0 和365 之间的某个随机数。如果你确实得到了365,那将使birthday[day] 抛出越界异常,因为你的数组的最大索引是364,而不是365。

您可能打算用这种方式读取随机值:

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

【讨论】:

    【解决方案2】:

    Java 中的数组是从零开始的。如果您创建长度为365 的birthday,则索引将从0 到364。

    你需要从这里改变这一行:

    int[] birthday = new int[YEAR];
    

    到这里:

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

    【讨论】:

    • 谢谢!这完全解决了我的问题!感谢您的帮助!
    • 它并不能完全解决问题,尽管它可以让你的程序运行。正如@sstan 所指出的,您正在生成从 0 到 365 的随机值——即 366 个不同的随机值。所以你的实验与原来的问题不匹配;它们的有效期为一年 366 天,而不是一年 365 天。 (如果你想计算 2 月 29 日,这没关系,但问题的通常表述方式并不计算在内。此外,如果你想包括 2 月 29 日,你必须对其加权,因为只发生 2/29 生日是其他日子的 97/400 倍。)
    • 哦,我明白了!那讲得通。感谢您的帮助。我很感激。
    猜你喜欢
    • 1970-01-01
    • 2021-04-30
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多