【发布时间】:2013-11-15 18:42:53
【问题描述】:
好的。我因为无法自己看到这一点而感到非常愚蠢,但我被困住了。第一个代码块与发生错误的其余代码相关。提前致谢! :D
此代码确定将出现多少怪物(数组的大小)。
public static byte monsters()
{
byte b = -7; //identifies how many monsters will be found
chance = (byte)d20.nextInt(30); //dice roll
if(chance >= 24)
{
b = 4; //quadruple monsters
}
else if(chance >= 18)
{
b = 3; //triple monsters
}
else if(chance >= 12)
{
b = 2; //double monsters
}
else if(chance >= 6)
{
b = 1; //single monster
}
else
{
b = 0; //no monster
}
return b;
}//end monsters()
此代码确定并生成要放入数组的怪物。第一部分从上述代码中获取输出以确定大小。第二部分生成怪物。当“NullPointerException”被抛出时,这是它指向的代码,特别是“for(x=0;x
public void determineMons()
{
byte x = 0; //counter
switch(monsters()) //defines the array
{
case 4:
monsters = new Monster[4];
break;
case 3:
monsters = new Monster[3];
break;
case 2:
monsters = new Monster[2];
break;
case 1:
monsters = new Monster[1];
break;
}//end switch
for(x=0;x<monsters.length;x++) //populates the array
{
chance = (byte)d20.nextInt(20); //dice roll
if(chance >= 15)
{
monsters[x] = new NazRuel();
}
else if(chance >= 10)
{
monsters[x] = new GiantSnake();
}
else if(chance >= 5)
{
monsters[x] = new Yeti();
}
else
{
monsters[x] = new Zombie();
}
}//end fill For
}//end determineMons()
这里是“ArrayOutOfBoundsExceptions”代码。错误在不同情况之间反弹,但每次错误行都是“monster =”行。
determineMons();
switch(Cell.monsters())
{
case 4:
monster = "There is a " + monsters[0] + ", a " + monsters[1] + ", a " + monsters[2] + ", and a " + monsters[3] + " in this area!";
break;
case 3:
monster = "There is a " + monsters[0] + ", a " + monsters[1] + ", and a " + monsters[2] + " in this area!";
break;
case 2:
monster = "There is a " + monsters[0] + ", and a " + monsters[1] + " in this area!";
break;
case 1:
monster = "There is a " + monsters[0] + " in this area!";
break;
}//end Monster block
【问题讨论】:
-
你考虑过调试吗?
-
不要切换一个值只是为了将该值用作文字:直接使用该值
-
啊,对不起。我什至没有在这里看到你们所有人的cmets。 Cell.monsters() 是发布的第一个代码块。我曾考虑过调试,但我不知道如何使用调试器,在课堂、工作和项目之间,我目前没有时间自学。谢谢你想帮忙。 :D
标签: java arrays nullpointerexception indexoutofboundsexception