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

时间:2015-08-29 03:54:00

标签: java arrays loops birthday-paradox

  

生日悖论说两个人在一个房间里的概率   会有同样的生日,只要数量的一半以上   房间里的人(n)超过23岁。这个属性并不是一个悖论,   但很多人发现它令人惊讶。设计一个可以的C ++程序   通过对随机生成的生日进行一系列实验来测试这个悖论,   测试这个矛盾的n = 5,10,15,20 ,. 。 。 ,你应该跑   对于每个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我无法弄清楚我的代码有什么问题

2 个答案:

答案 0 :(得分:2)

如果您提供了引发异常的确切行号(信息在您获得的错误堆栈跟踪中),那将是很好的,但很可能在此处出现问题:

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

Random.nextInt的文档说:

  

返回:下一个伪随机数,在此随机数生成器的序列中,在零(包含)和绑定(不包含)之间均匀分布的int值。

在这种情况下,您使用Random.nextInt366)来调用365 + 1,这意味着您有效地在0365之间读取一些随机数。 365。如果你得到birthday[day],那将使364抛出越界异常,因为数组的最大索引是365,而不是int day = randNum.nextInt(YEAR); // 365 (exclusive)

你可能想以这种方式读取随机值:

$app->post('/check/username/availability', function() use($app) {
    $db = new db();
    $db->setErrorCallbackFunction("myErrorHandler", "text");
    $request = \Slim\Slim::getInstance()->request()->post();
    $app = \Slim\Slim::getInstance();
        $bind = array(
            ":un" => $request['txt_un']
        );
        $result = $db->select("accounts", "username = :un", $bind);
        if(count($result) > 0) {
            echo 1;
        } else {
            echo 2;
        }
});

答案 1 :(得分:1)

Java中的数组是基于零的。如果您创建长度为@reboot sudo /root/.nvm/v0.10.26/bin/node /root/tweetmonkey-raspi &的{​​{1}},则索引将从ps -ebirthday

您需要更改此行:

365

对此:

0