如何修复 ArrayIndexOutOfBoundsException(控制台)
How to fix ArrayIndexOutOfBoundsException (console)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at com.PenguinGaming.Player.User(Player.java:47)
at com.PenguinGaming.Game.main(Game.java:18)
不知道如何防止 6 后崩溃。我想说那个位置不可去。我试过了
if(input.equals(commands.commandlist()[1])){
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
if(playerY > 6){
playerY -=1;
System.out.println("You can not move in that direction");
}
playerY += 1;
}
我只在 GUI 中处理过一次这个问题,那是在观看视频之后,我们是如何做到的,一旦它到达就摧毁了对象。在这种情况下,我不想破坏它,但是,说它不能再继续了。我认为问题出在我的循环中,它添加了一个然后检查所以它超出并崩溃了。为了防止这种情况,我可能需要先检查但不确定如何检查。
Player.java
package com.PenguinGaming;
import java.util.Scanner;
public class Player {
public void User(){
Commands commands = new Commands();
Map map = new Map();
int Maxlife = 25;
int life = 25;
int turns = 0;
//Starting player location
int playerX = 4;
int playerY = 4;
Scanner scanner = new Scanner(System.in);
String input = null;
while(life > 1){
System.out.println("Player life: " + life + "/" + Maxlife);
input = scanner.nextLine();
input = input.toLowerCase();
life -= 1;
for(int i = 0; i == 30; i++){
turns++;
if(turns == 30){
Maxlife += 5;
}
}
if (input.equals(commands.commandlist()[0])) {
// give description for player location
System.out.println(map.MapLayout()[playerX][playerY]);
}
if(input.equals(commands.commandlist()[1])){
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
playerY += 1;
}
if(input.equals(commands.commandlist()[2])){
//Go east
System.out.println(map.MapLayout()[playerX+1][playerY]);
playerX += 1;
}
if(input.equals(commands.commandlist()[3])){
//Go south
System.out.println(map.MapLayout()[playerX][playerY-1]);
playerY -= 1;
}
if(input.equals(commands.commandlist()[4])){
//Go west
System.out.println(map.MapLayout()[playerX-1][playerY]);
playerX -= 1;
}
if(input.equals(commands.commandlist()[5])){
//Terminate game
break;
}
if(life <= 0){
System.out.println("Looks like you have starved, better luck next game");
break;
}
if(playerY > 6|| playerX > 6){
System.out.println("You can not move in that direction");
}
}
System.exit(0);
}
}
Game.java
package com.PenguinGaming;
import java.util.Scanner;
public class Game{
public static void main(String[] args){
Player player = new Player();
Map map = new Map();
boolean running = true;
//Print starting location
System.out.println(map.MapLayout()[4][4]);
while(running){
player.User();
}
}
}
你应该在这行之前检查 playerY
System.out.println(map.MapLayout()[playerX][playerY+1]);
即:
if(input.equals(commands.commandlist()[1])){
if(playerY > 6){
playerY -=1;
System.out.println("You can not move in that direction");
} else {
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
playerY += 1;
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at com.PenguinGaming.Player.User(Player.java:47)
at com.PenguinGaming.Game.main(Game.java:18)
不知道如何防止 6 后崩溃。我想说那个位置不可去。我试过了
if(input.equals(commands.commandlist()[1])){
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
if(playerY > 6){
playerY -=1;
System.out.println("You can not move in that direction");
}
playerY += 1;
}
我只在 GUI 中处理过一次这个问题,那是在观看视频之后,我们是如何做到的,一旦它到达就摧毁了对象。在这种情况下,我不想破坏它,但是,说它不能再继续了。我认为问题出在我的循环中,它添加了一个然后检查所以它超出并崩溃了。为了防止这种情况,我可能需要先检查但不确定如何检查。
Player.java
package com.PenguinGaming;
import java.util.Scanner;
public class Player {
public void User(){
Commands commands = new Commands();
Map map = new Map();
int Maxlife = 25;
int life = 25;
int turns = 0;
//Starting player location
int playerX = 4;
int playerY = 4;
Scanner scanner = new Scanner(System.in);
String input = null;
while(life > 1){
System.out.println("Player life: " + life + "/" + Maxlife);
input = scanner.nextLine();
input = input.toLowerCase();
life -= 1;
for(int i = 0; i == 30; i++){
turns++;
if(turns == 30){
Maxlife += 5;
}
}
if (input.equals(commands.commandlist()[0])) {
// give description for player location
System.out.println(map.MapLayout()[playerX][playerY]);
}
if(input.equals(commands.commandlist()[1])){
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
playerY += 1;
}
if(input.equals(commands.commandlist()[2])){
//Go east
System.out.println(map.MapLayout()[playerX+1][playerY]);
playerX += 1;
}
if(input.equals(commands.commandlist()[3])){
//Go south
System.out.println(map.MapLayout()[playerX][playerY-1]);
playerY -= 1;
}
if(input.equals(commands.commandlist()[4])){
//Go west
System.out.println(map.MapLayout()[playerX-1][playerY]);
playerX -= 1;
}
if(input.equals(commands.commandlist()[5])){
//Terminate game
break;
}
if(life <= 0){
System.out.println("Looks like you have starved, better luck next game");
break;
}
if(playerY > 6|| playerX > 6){
System.out.println("You can not move in that direction");
}
}
System.exit(0);
}
}
Game.java
package com.PenguinGaming;
import java.util.Scanner;
public class Game{
public static void main(String[] args){
Player player = new Player();
Map map = new Map();
boolean running = true;
//Print starting location
System.out.println(map.MapLayout()[4][4]);
while(running){
player.User();
}
}
}
你应该在这行之前检查 playerY
System.out.println(map.MapLayout()[playerX][playerY+1]);
即:
if(input.equals(commands.commandlist()[1])){
if(playerY > 6){
playerY -=1;
System.out.println("You can not move in that direction");
} else {
//Go north
System.out.println(map.MapLayout()[playerX][playerY+1]);
playerY += 1;
}
}