尝试设计一个硬币翻转程序,要求用户说明他们想要翻转硬币多少次(翻转次数必须小于1000)。然后,我得到一个从1到10的随机数,并根据用户喜欢的翻转次数将该数字分配给声明的每个数组索引。

我似乎遇到了三个错误,其中涉及无法解析math.random行上的符号。任何帮助,将不胜感激。

import java.io.*;
import java.util.*;

public class coinFlip {

public static void main(String[] args) throws IOException {

    // declare in as a BufferedReader; used to gain input from the user
    BufferedReader in;
    in = new BufferedReader(new InputStreamReader(System.in));

    //declare variables
    int flips;
    int anArray[];
    int x;
    int r;

    System.out.println("How many times would you like to flip your coin?");
    flips = Integer.parseInt(in.readLine());

    if(flips <= 1000) {
        System.out.println("You want to flip " + flips + " times");
        anArray = new int[flips];

        for(x = 0; x <= flips; x++) {
            r = Math.round(Math.random()*9)+1;
            anArray[x] = r;
            System.out.println(anArray[x]);
        }
    }

  }

}

最佳答案

for(x = 0; x <= flips; x++)

应该

for(x = 0; x < flips; x++)

flips[1000]是第1001个插槽,太多了。

07-27 18:03