java - 我的数组索引超出范围,但为什么呢?
问题描述
我收到错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at score.checkWinnings(score.java:29)
at yahtzee.main(yahtzee.java:52)
主要的第 52 行是:
points.checkWinnings(Dice, wins);
分数中的第 29 行是:
if (Dice[y] == 1)
我正在尝试获得掷骰子的分数,但不确定为什么 第 29 和 52 行会搞砸。我努力了:
for (y = 0; y < Dice[y]; y++);
//this one still gives me an error
for (y = 0; y < 5; y++);
//this one only works when I input that I want to use 5 dice
有谁知道为什么我拥有/我尝试过的东西不起作用?任何帮助将不胜感激!
我在下面包含了我的代码的相关部分:
主要的:
import java.util.ArrayList;
public class yahtzee
{
public static void main(String[] args)
{
int play = 1, scorea = 0, sum = 0;
int[] wins = new int[15];
while ((play == 1) && (sum < 15))
{
yahtzeeConfig config= new yahtzeeConfig();
config.start();
int[] Dice = new int[config.hand];
//hand in score
sum = 0;;
int roll = 0;
int x, z;
int rerolla = 0;
dice die = new dice();
System.out.println("\nHere is your roll:\n");
//sets the dice values
ArrayList<Integer> arList= new ArrayList<Integer>(config.hand);
for (x = 0; x < config.hand; x++)
{
die.roll();
Dice[x] = die.get();
arList.add(Dice[x]);
}
//prints out dice values
for (int i =0; i< config.hand; i++)
{
System.out.println("Die " + (i+1) + ": " + Dice[i]);
}
//re-rolls dice
do {
for (int i =0; i< config.hand; i++)
{
play = inputInt("\nWould you like to reroll " + Dice[i] + " ? (1=yes, 2=no)");
if (play == 1)
{
Dice[i]= die.roll();
//roll++;
}
System.out.println("Die " + (i+1) + ": " + Dice[i]);
}
}
//checks score
while ((roll < 2) && (rerolla > 0));
score points = new score();
points.checkWinnings(Dice, wins);
for (z = 0; z < 15; z++) {
sum += wins[z];
}
scorea += points.return_score();
System.out.println("Your total score is: " + scorea);
分数等级:
public class score
{
private int score;
public score()
{
score = 0;
}
public void checkWinnings(int[] Dice, int[] wins)
{
System.out.println("\nLet's Check Score: \n ");
int x = 0, y = 0, winings = 0, winingsa = 0;
int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;
//counts numbers
for (y = 0; y < Dice.length; y++);
//for (y = 0; y < Dice[y]; y++);
//for (y = 0; y < 5; y++);
{
if (Dice[y] == 1)
{
ones++;
}
if (Dice[y] == 2)
{
twos++;
}
if (Dice[y] == 3)
{
threes++;
}
if (Dice[y] == 4)
{
fours++;
}
if (Dice[y] == 5)
{
fives++;
}
if (Dice[y] == 6)
{
sixes++;
}
}
//upper total
System.out.println ("Ones: " + ones);
System.out.println("Twos: " + twos);
System.out.println("Threes: " + threes);
System.out.println("Fours: " + fours);
System.out.println("Fives: " + fives);
System.out.println("Sixes: " + sixes + "\n");
}
解决方案
评论中有很多很好的调试 - 你的问题是你为什么得到 ArrayIndexOutOfBounds - 因为数组在 java 中是 0'indexed ...
即大小为 5 的数组只能通过 [0]、[1]、[2]、[3]、[4] 访问。
你有错误索引的具体原因 - 我想有几个人已经为你找到了。
通常,意外的 IndexOutOfBoundsException 很可能是错误的指示(所依赖的值实际上与预期不匹配或算法问题)。
推荐阅读
- c++ - 使用外部全局变量初始化全局变量
- android - 我无法在 android 模拟器上运行我的颤振项目
- python - 如何将整个索引数据从一台 Splunk 服务器迁移到另一台 Splunk 服务器
- react-native - 在 Testflight 上展示不同的应用程序环境,并在发布时更改它
- python - 如何检测 Python 是否在 Git Bash 终端或 Windows cmd 中运行?
- c++ - 复杂输入的性能问题 - 解释和解析树的替代方案
- mysql - 在Mysql中添加额外的列和行来计算每个表行和列的sum()
- javascript - 如何在录制停止后立即显示录制的长度和下载选项?
- python - 如何使用 python selenium 单击 Hcaptcha 复选框
- java - Spring Boot @Getmapping 在一个非常简单的项目中返回 404 whitelabel 错误页面