gpt4 book ai didi

java - 索引越界错误在显示时会有所不同

转载 作者:行者123 更新时间:2023-11-29 08:50:56 24 4
gpt4 key购买 nike

当我定期运行这个程序时,我不断收到索引越界错误。当我输入更高的起始百分比(如 80+)时,我会更加注意到它。我知道它与 shotsMade ArrayList 有关。但我不确定该怎么做才能纠正它。谢谢。

    import java.util.*;

public class Test {
public static void main(String[] args) {
int myGameCounter = 1;
List<Integer> shotsMade = new ArrayList<Integer>();
shotsMade.add(0);
System.out.print("Enter Player's Free Throw Percentage: ");
Scanner input = new Scanner(System.in);
int percent = input.nextInt();


//Game #1
System.out.println("Game " + myGameCounter + ":");
Random r = new Random();
myGameCounter++;
for (int i = 0; i < 10; ++i){
boolean in = tryFreeThrow(percent);
if (in) {
shotsMade.set(0, shotsMade.get(0)+1);
System.out.print("In" + " ");
}
else {
shotsMade.add(0);
System.out.print("Out" + " ");
}
}
System.out.println("");
System.out.println("Free throws made: " + shotsMade.get(0) + " out of 10");
//Game #2
System.out.println("");
System.out.println("Game" + myGameCounter + ":");
myGameCounter++;
for (int i = 0; i < 10; ++i){
boolean in = tryFreeThrow(percent);
if (in) {
shotsMade.set(1, shotsMade.get(1)+1);
System.out.print("In" + " ");
}
else {
shotsMade.add(0);
System.out.print("Out" + " ");
}
}
System.out.println("");
System.out.println("Free throws made: " + shotsMade.get(1) + " out of 10");
//Game #3
System.out.println("");
System.out.println("Game" + myGameCounter + ":");
myGameCounter++;
for (int i = 0; i < 10; ++i){
boolean in = tryFreeThrow(percent);
if (in) {
shotsMade.set(2, shotsMade.get(2)+1);
System.out.print("In" + " ");
}
else {
shotsMade.add(0);
System.out.print("Out" + " ");
}
}
System.out.println("");
System.out.println("Free throws made: " + shotsMade.get(2) + " out of 10");
//Game #4
System.out.println("");
System.out.println("Game" + myGameCounter + ":");
myGameCounter++;
for (int i = 0; i < 10; ++i){
boolean in = tryFreeThrow(percent);
if (in) {
shotsMade.set(3, shotsMade.get(3)+1);
System.out.print("In" + " ");
}
else {
shotsMade.add(0);
System.out.print("Out" + " ");
}
}
System.out.println("");
System.out.println("Free throws made: " + shotsMade.get(3) + " out of 10");
//Game #5
System.out.println("");
System.out.println("Game" + myGameCounter + ":");
myGameCounter++;
for (int i = 0; i < 10; ++i){
boolean in = tryFreeThrow(percent);
if (in) {
shotsMade.set(4, shotsMade.get(4)+1);
System.out.print("In" + " ");
}
else {
shotsMade.add(0);
System.out.print("Out" + " ");
}
}
System.out.println("");
System.out.println("Free throws made: " + shotsMade.get(4) + " out of 10");

System.out.println("");
System.out.println("Summary:");


}//main



public static boolean tryFreeThrow(int percent) {
Random r = new Random();
int number = r.nextInt(100);
if (number > percent){
return false;
}
return true;
}






}//class

最佳答案

不运行代码,但看起来如果您运行游戏并获得 10 In,然后在第二场比赛中获得 In,那么您永远不会向数组列表添加第二个条目..

因此第二个游戏会在你调用时抛出一个IndexOutOfBoundsException

shotsMade.set(1, shotsMade.get(1)+1);

第二局

关于java - 索引越界错误在显示时会有所不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22706339/

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