gpt4 book ai didi

java - 索引越界异常java

转载 作者:行者123 更新时间:2023-11-29 08:27:59 26 4
gpt4 key购买 nike

我对数组递增有疑问。

我的播放器逻辑类是:

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

当增加 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));

最佳答案

你这里有一个逻辑错误,

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

您的if只有在serverreceiver得分大于或等于三。当 eithertrue 时,您想输入该逻辑。喜欢,

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 block - 因为我们会在满足条件时 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();
}

关于java - 索引越界异常java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com