(Я использую Java 1.4.2) Я хотел иметь массив, который должен перевернуть «монету» определенное количество раз, заданное пользователем (монета может переворачиваться до 1000 раз). Затем произвольно генерируйте целое число от 1 до 10, сохраняйте все числа в массиве. У меня есть следующий код, но он продолжает давать мне следующую ошибку:Java 1.4.2 - Ошибка ArrayIndexOutOfBounds
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at 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]);
}
}
}
В следующий раз, пожалуйста, не заставляй нас рассчитывать (или разумно попытаться разглядеть), где строка 42 есть. Кроме того, спасибо за создание [line 42] (http://bit.ly/kQG2PA). :-) – jason