作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我定期运行这个程序时,我不断收到索引越界错误。当我输入更高的起始百分比(如 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/
我创建了一个分支来开发新功能。由于这个新功能完全是作为一个新项目开发的,唯一可能的冲突来源是解决方案文件。 随着功能的开发,主分支更新了几次。当我完成开发和测试时,我做了: git checkout
我是一名优秀的程序员,十分优秀!