首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Pokemon程序

Java Pokemon程序
EN

Stack Overflow用户
提问于 2017-09-17 17:30:10
回答 1查看 149关注 0票数 0

我正在编写一个简单的类似口袋妖怪的程序,但当用户指定他们希望在团队中加入多少口袋妖怪时,我遇到了一个问题。用户团队拥有一张Pokemon名称和Pokemon对象本身的地图。

当游戏开始时,用户指定他们想在团队中加入多少口袋妖怪。有一个预先制作的6个口袋妖怪阵列。默认的Pokemon构造函数为2e14和-2e14之间的随机双变量分配名称。然后,for循环将指定数量的Pokemon对象添加到用户的团队中,并请求每个对象的统计数据。请求统计数据的循环--然后是--应该是,对于每个条目,删除它,然后用与输入的口袋妖怪名称相对应的键返回一个条目。

我目前有以下问题:- ConcurrentModificationException

编辑:解决。相反,我使用预先生成的pokemons数组中的对象获取统计数据,然后,一旦它们有了统计数据,就将它们添加到团队中,这样我就不必更改密钥了。这里是违规代码:

代码语言:javascript
复制
// 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);
        }

    }

如果您想要的话,下面是一些附加的代码片段:

代码语言:javascript
复制
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号添加到用户的团队中:

代码语言:javascript
复制
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和一个方法可以将口袋妖怪添加到其中:

代码语言:javascript
复制
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():

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-17 17:48:01

除非您更改pickedPokemon的值,否则它将永远打印Error: Pokemon not found. Try again.

代码语言:javascript
复制
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.");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46266916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档