索引越界异常java

索引越界异常java

本文介绍了索引越界异常java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组递增的问题.

我的播放器逻辑类是:

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 -> 40:40 -> PlayerOne winBall -> Advantage:40 -> PlayerTwo winBall -> 40:40 -> PlayerTwo winBall -> 40:Advantage

Its a tennis game so there is posibility of score:PlayerOne:PlayerTwo -> 40:40 -> PlayerOne winBall -> Advantage:40 -> PlayerTwo winBall -> 40:40 -> PlayerTwo winBall -> 40:Advantage

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

When incremented score for PlayerTwo i should get PlayerTwo Won but instead of correct incrementation i have that:

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)

我知道问题出在数组长度上,但我不知道如何解决.

I know the problem is array length but i have no idea how to fix it.

主代码:

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));

推荐答案

首先你有逻辑问题,

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

如果一名球员在2分上占优势,并不意味着他赢得了比赛,因为比分可能是30:0、0:30、40:15等.
相反,我建议将游戏状态保留在 TennisGame 类中:

if one player have advantage in 2 points, it doesn't mean he wins the game, as the score can be 30:0, 0:30, 40:15, etc.
Instead of this I suggest to keep the status of the game inside of TennisGame class:

public boolean isFinished;

public boolean isFinished() {
    return isFinished;
}

并在其中一名玩家赢得比赛时设置它并在此处使用它,如下所示:

and set it when one of the players win the game and use it here like this:

} while(!tennisGame.isFinished)

其次为了避免崩溃并遵循网球比赛逻辑,我建议做以下更改:

Second to avoid crashes and follow the tennis game logic I suggest to do the following changes:

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

还做了一些优化,避免了很多变量的使用:

Also done some optimizations to avoid to many variable usages:

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;
    }

这篇关于索引越界异常java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:42