gpt4 book ai didi

java - 因未知原因而出现错误

转载 作者:行者123 更新时间:2023-12-01 07:27:49 26 4
gpt4 key购买 nike

尝试设计一个硬币翻转程序,要求用户说明他们想要翻转硬币的次数(翻转次数必须低于 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 个槽位,太多了。

关于java - 因未知原因而出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com