-4

(我正在使用 Java 1.4.2)我想要一个数组,它应该在用户给定的一定时间内翻转“硬币”(硬币最多可以翻转 1000 次)。然后随机生成一个 1 到 10 之间的整数,将所有数字存储在一个数组中。我有以下代码,但它不断给我以下错误:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 在 CoinFlip.main(CoinFlip.java:42)

这是我写的代码。

import java.io.*;
import java.math.*;

public class CoinFlip
{
    public static void main(String[] args) throws IOException
    {
        // setting up variables
        String timesString;
        int times;
        // setting up input
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(System.in));


        do
        {
            // times will display how many times the coin will flip
            // loop if times does not fulfill the requirements
            System.out.println("How many times do you want the coin to flip? (MUST be an integer between 1 to 1000)");
            timesString = in.readLine();
            // convert timesString into an integer
            times = Integer.valueOf(timesString).intValue();
            if((times > 1000)||(times < 1))
            {
                System.out.println("ERROR: The number of times you flip the coin must be an integer between 1 and 1000.");
            }
            System.out.println("The value for times is " +times);
        }
        while((times > 1000)||(times < 1));


        // create a new array
        double flip[] = new double[times];

        // create a new variable timeStore that sets the boolean conditions for the For Loop
        int timeStore;
        for(timeStore = 0; timeStore <= times-1; timeStore++)
        {
            System.out.println("timeStore (how many iterations) is " +timeStore);
            System.out.println("FOR LOOP:  When " +timeStore+ " is less than or equal to " +(times-1));
            flip[times] = Math.round(Math.random()*9)+1;
            // the line above stores a random integer between 1 and 10 within the current index
            System.out.println("flip["+times+"] = "+flip[times]);
        }

    }
}
4

2 回答 2

5

在第 42 行;

flip[times] = Math.round(Math.random()*9)+1;

应该;

flip[timeStore] = Math.round(Math.random()*9)+1;

然后也在第 44 行的 System.out 中。

于 2013-08-07T15:09:43.543 回答
2

这些是错误的:

42: flip[times] = Math.round(Math. random()* 9 ) + 1;
44: System.out.println("flip[" + times + "] = " + flip[times]);

看来你的意思

42: flip[timeStore] = Math.round(Math.random()*9)+1;
44: System.out.println("flip["+timeStore+"] = "+flip[timeStore]);

由于您声明flip为 size 数组times,因此只有索引0...(times - 1)有效。特别是,索引times是无效的,所以flip[times]会抛出。

Note that the compiler was telling you the offending line, and the offending index passed in. It's telling you you passed in 7 as the index. But note, this exception is happening on the first iteration of your loop. You can tell this by the output that your program would have produced since you have helpful print debugging statements in your code. Thus, you should have been able to reason: hey, why is my array seeing the index 7 on the first iteration of the loop when the index should be 0? At that point, the error practically figures itself out.

Also, idiomatic usage of arrays is:

for(timeStore = 0; timeStore < times; timeStore++)

but even better is:

for(timeStore = 0; timeStore < flip.length; timeStore++)
于 2013-08-07T15:09:53.373 回答