索引超出范围的异常java

时间:2018-06-10 12:55:29

标签: java exception arraylist indexoutofboundsexception

我有阵列增量问题。

我的玩家逻辑课程是:

public class Player {
public static final List<String> pointsDescriptions  = Arrays.asList("0", "15", "30", "40");

private int score;

public int getScore(){return score;}

String name;
public String getName(){return name;}

public void winBall(){this.score += 1;}

public Player(String name){this.name = name;}

public String getScoreDescription(){return pointsDescriptions.get(score);}
}

游戏逻辑是:

public class TennisGame {

private Player server;
private Player receiver;

public TennisGame(Player server, Player receiver){
    this.server = server;
    this.receiver = receiver;
}

public String getScore(){
    if (server.getScore() >= 3 && receiver.getScore() >= 3){
        if(Math.abs(receiver.getScore() - server.getScore()) >= 2){
            String winner;
            winner = getLeadPlayer().getName() + " won";
            return winner;
        } else if (server.getScore() - receiver.getScore() >= 1) {
            String serverAdvantage;
            serverAdvantage = "A" + ":" + receiver.getScoreDescription();
            return serverAdvantage;
        } else if (receiver.getScore() - server.getScore() >= 1) {
            String receiverAdvantage;
            receiverAdvantage = server.getScoreDescription() + ":" + "A";
            return receiverAdvantage;
        } else {
            String deuce;
            deuce = "40:40";
            return deuce;
        }
    } else {
        return server.getScoreDescription() + ":" + receiver.getScoreDescription();
    }
}

public Player getLeadPlayer() {
    return (server.getScore() > receiver.getScore()) ? server : receiver;
}
}

这是一场网球比赛,所以有得分: PlayerOne:PlayerTwo - &gt; 40:40 - &gt; PlayerOne winBall - &gt;优点:40 - &gt; PlayerTwo winBall - &gt; 40:40 - &gt; PlayerTwo winBall - &gt; 40:优势

当PlayerTwo增加得分时,我应该获得PlayerTwo Won,但不是正确的增量,我有:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at java.util.Arrays$ArrayList.get(Arrays.java:3841)
    at com.company.Main.Player.getScoreDescription(Player.java:30)
    at com.company.Main.TennisGame.getScore(TennisGame.java:23)
    at com.company.Main.Main.main(Main.java:33)

我知道问题是数组长度,但我不知道如何解决它。

主要代码:

Scanner s = new Scanner(System.in);
    Player server = new Player("server");
    Player receiver = new Player("receiver");
    TennisGame tennisGame = new TennisGame(server, receiver);

    System.out.println("Server=1");
    System.out.println("Server=2");
    System.out.println("Please enter the player that wins the point");
    System.out.println("The score is: " + tennisGame.getScore());

    do {
        tennisGame.getScore();
        String userInput = s.nextLine();
        if (userInput.equals("1")) {
            server.winBall();
        } else if (userInput.equals("2")) {
            receiver.winBall();
        } else {
            System.out.println("Wrong value, please enter 1 or 2");
        }
        System.out.println(tennisGame.getScore());
    } while (!(Math.abs(receiver.getScore() - server.getScore()) >= 2));

2 个答案:

答案 0 :(得分:2)

这里有一个逻辑错误,

if (server.getScore() >= 3 && receiver.getScore() >= 3){

只有ifserver 得分大于或等于3时,才会输入receiver。当 true时,您想要输入该逻辑。像,

if (server.getScore() >= 3 || receiver.getScore() >= 3){

,我建议您删除return分支机构中的临时变量。像,

if(Math.abs(receiver.getScore() - server.getScore()) >= 2){
    return getLeadPlayer().getName() + " won";
} else if (server.getScore() - receiver.getScore() >= 1) {
    return "A" + ":" + receiver.getScoreDescription();
} else if (receiver.getScore() - server.getScore() >= 1) {
    return server.getScoreDescription() + ":" + "A";
} else {
    return "40:40";
}

,我们可以使用一对局部变量简化。我们可以消除一些else块 - 因为我们在满足条件时return。像,

public String getScore() {
    final int rScore = receiver.getScore(), sScore = server.getScore();
    if (sScore >= 3 || rScore >= 3) {
        if (Math.abs(rScore - sScore) >= 2) {
            return getLeadPlayer().getName() + " won";
        } else if (sScore - rScore >= 1) {
            return "A:" + receiver.getScoreDescription();
        } else if (rScore - sScore >= 1) {
            return server.getScoreDescription() + ":A";
        }
        return "40:40";
    }
    return server.getScoreDescription() + ":" + receiver.getScoreDescription();
}

答案 1 :(得分:2)

首先,你有逻辑问题,

} while (!(Math.abs(receiver.getScore() - server.getScore()) >= 2));

如果一名球员在2分中有优势,那并不意味着他赢了比赛,因为比分可以是30:0,0:40,40:15等。
而不是这个我建议保持在TennisGame类中的游戏状态:

public boolean isFinished;

public boolean isFinished() {
    return isFinished;
}

并在其中一名玩家赢得比赛并在此处使用时设置:

} while(!tennisGame.isFinished)

第二,为避免崩溃并遵循网球游戏逻辑,我建议做以下更改:

public static final List<String> pointsDescriptions  = Arrays.asList("0", "15", "30", "40", "A");

还做了一些优化以避免许多变量用法:

public String getScore(){
        String score;
        if (server.getScore() >= 3 && receiver.getScore() >= 3){
            if(Math.abs(receiver.getScore() - server.getScore()) >= 2){
                score = getLeadPlayer().getName() + " won";
                isFinished = true;
            } else if (server.getScore() - receiver.getScore() >= 1) {
                score = "A" + ":" + receiver.getScoreDescription();
            } else if (receiver.getScore() - server.getScore() >= 1) {
                score = server.getScoreDescription() + ":" + "A";
            } else {
                score = "40:40";
            }
        } else if (server.getScore() > 3 || receiver.getScore() > 3) {
            score = getLeadPlayer().getName() + " won";
            isFinished = true;
        } else {
            score = server.getScoreDescription() + ":" + receiver.getScoreDescription();
        }
        return score;
    }