gpt4 book ai didi

Java Pokemon 程序 --> 删除 map 条目时出现并发修改异常

转载 作者:行者123 更新时间:2023-12-02 12:14:49 25 4
gpt4 key购买 nike

我正在编写一个简单的类似 Pokemon 的程序,但是当用户指定他们想要在团队中加入多少 Pokemon 时,我遇到了一个问题。用户的团队拥有 Pokemon 名称和 Pokemon 对象本身的 map 。

当游戏开始时,用户指定他们想要加入团队的神奇宝贝数量。有 6 个 Pokemon 的预制阵列。默认的 Pokemon 构造函数将名称分配给 2e14 和 -2e14 之间的随机 double 值。然后,for 循环将指定数量的 Pokemon 对象添加到用户的团队中,并询问每个对象的统计信息。询问统计数据的循环应该,对于每个条目,将其删除并放回到具有与输入的神奇宝贝名称相对应的 key 的条目中。

我目前遇到以下问题:- 并发修改异常

编辑:已解决。相反,我使用预先制作的可能的口袋妖怪数组中的对象获取统计数据,然后一旦它们有了统计数据,将它们添加到团队中,这样我就不必更改 key 。这是有问题的代码:

// for every Pokemon on the user's team, get the stats for them.
// Logic:
/* The for loop goes through the Map. For each entry, it saves the name of the pokemon
* and the pokemon itself, because if you don't, the key is just a random double, so
* the user can't call its name. This goes through and removes those entries,
* then re - inserts them back into the team, but this time the key corresponds to its name */
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
// if the pokemon hasn't already gone through this procedure,
if(!entry.getValue().hasBeenStats) {
entry.getValue().getStats(input);
Pokemon pok = entry.getValue();
userTeam.team.remove(entry.getKey());
userTeam.addPokemon(pok);
}

}

如果您需要,这里有一些额外的代码片段:

public class PokemonGame {
int userTeamSize;
PokemonTeam userTeam = new PokemonTeam();

// potential pokemon
Pokemon pok1 = new Pokemon();
Pokemon pok2 = new Pokemon();
Pokemon pok3 = new Pokemon();
Pokemon pok4 = new Pokemon();
Pokemon pok5 = new Pokemon();
Pokemon pok6 = new Pokemon();

// 0 1 2 3 4 5
Pokemon[] potentialUserPokemons = {pok1, pok2, pok3, pok4, pok5, pok6};

以下方法应该接收一个数字,该数字将成为 teamSize,然后将该数量的 Pokemon 从数组添加到用户的团队中:

private void getUserSettings(Scanner input, PokemonTeam team) {
System.out.println("How many Pokemon do you want on your team?");

while(true) {
try {
int tempInt = Integer.parseInt(input.next());
if((tempInt > 6) || (tempInt < 1) ) {
System.out.println("Error: Enter valid team length.");
continue;
} else {
userTeamSize = tempInt;
}
break;
} catch(NumberFormatException e) {
System.out.println("Error: Try again");
continue;
}
}
input.nextLine();
}

private void setUpUserTeam(Scanner input) {
/* adds every Pokemon for the specified length into the users team, from the
* potential team array*/
for(int num = 0; num < userTeamSize; num++) {
userTeam.addPokemon(potentialUserPokemons[num]);
}
// for every Pokemon on the user's team, get the stats for them.
// Logic:
/* The for loop goes through the Map. For each entry, it saves the name of the pokemon
* and the pokemon itself, because if you don't, the key is just a random double, so
* the user can't call its name. This goes through and removes those entries,
* then re - inserts them back into the team, but this time the key corresponds to its name */
// this part is not working, it only run once
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
// if the pokemon hasn't already gone through this procedure,
entry.getValue().getStats(input);
// save it's name --> key, and object --> pokemon for the value
String pokName = entry.getValue().getName();
Pokemon pok = entry.getValue();
userTeam.team.put(pokName, pok );
userTeam.team.remove(entry.getKey());
}

System.out.println("Pick a Pokemon to start with: ");
String pickedPokemon = input.nextLine();
// goes through the user's team, finds the Pokemon they specified, and sets it as the current pokemon
outerloop:
while (true) {
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
if(entry.getKey().equals(pickedPokemon)) {
userTeam.setCurrentPokemon(entry.getValue());
break outerloop;
}
}
System.out.println("Error: Pokemon not found. Try again.");
}
}

在 PokemonTeam 中,有一个 Map 和一个向其中添加 Pokemon 的方法:

Map<String, Pokemon> team = new HashMap<String, Pokemon>();
public void addPokemon(Pokemon pokemon) {
team.put(pokemon.getName(), pokemon);
/*teamSize is a different variable in PokemonTeam and once the
* Pokemons are added to the Map, will be the same as userTeamSize
* in class PokemonGame*/
teamSize = team.size();
}

这是 Pokemon 类中的 getStats():

public void getStats(Scanner theInput) {
System.out.println("Please enter the stats of your pokemon: ");
System.out.println("Name: ");
// set the pokemon's name as what they enter
this.setName(theInput.nextLine());
// error handling
System.out.println("Level: ");
while(true) {
// if there is a wrong type entered it will repeat until correct
try {
this.setLevel(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}

System.out.println("Attack: ");
while(true) {
try {
this.setAttack(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}

System.out.println("Defense: ");
while(true) {
try {
this.setDefense(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}

System.out.println("Base: ");
while(true) {
try {
this.setBase(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}

System.out.println("STAB: ");
while(true) {
try {
this.setSTAB(Integer.parseInt(theInput.next()));
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}

System.out.println("HP: ");
while(true) {
try {
int userHP = Integer.parseInt(theInput.next());
this.setMaxHP(userHP);
this.setCurrentHP(userHP);
this.setDamageAuto();
} catch(NumberFormatException e) {
System.out.println("Error: Please try again.");
continue;
}
break;
}
// gets the names of the moves and adds them to the map of moves and move infos
theInput.nextLine();
System.out.println("Name your Pokemon's 4 moves: ");
String moveNameOne = theInput.nextLine();
moves.put(moveNameOne, generateMoveInfo(moveNameOne));
String moveNameTwo = theInput.nextLine();
moves.put(moveNameTwo, generateMoveInfo(moveNameTwo));
String moveNameThree = theInput.nextLine();
moves.put(moveNameThree, generateMoveInfo(moveNameThree));
String moveNameFour = theInput.nextLine();
moves.put(moveNameFour, generateMoveInfo(moveNameFour));
hasBeenStats = true;
}

最佳答案

除非您更改 pickedPokemon 的值,否则将打印 Error: Pokemon not find。再试一次。永远。

while (true) {
for(Map.Entry<String, Pokemon> entry : userTeam.team.entrySet()) {
if(entry.getKey().equals(pickedPokemon)) {
userTeam.setCurrentPokemon(entry.getValue());
break outerloop;
}
}
System.out.println("Error: Pokemon not found. Try again.");
}

关于Java Pokemon 程序 --> 删除 map 条目时出现并发修改异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266916/

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