相当于Java中的'where'fortran关键字?

时间:2012-07-11 08:59:04

标签: java fortran where equivalent

我正在编写一个Java程序,我正在检查字符串中的列表,然后对其进行处理。在fortran中,我会按照

的方式写一些东西
where(list(:)==stringToCheck){
    ...
    statements
    ...
    }

相反,如果有各种各样的staments和break,我会头疼一块for循环。不,或许我可以稍微修改一下代码,但它仍然比Fortran感觉效率低得多。

编辑,这是我使用的代码:

for(int idx=0;idx<player.get_charactersOwned().size();idx++)
            {
                if(player.get_charactersOwned().get(idx).get_characterName().equals(charName))
                {
                    /* Add character to the game
                     * Add game to the character*/
                    System.out.println("Character "+charName+" Found ");
                    gameToMake.addCharacters(player.get_charactersOwned().get(idx));
                    player.get_charactersOwned().get(idx).addGame(gameToMake);
                    break;
                }else 
                {
                    System.err.println("Character "+ charName +" not found");
                    System.out.println("Shall I add that Character? y/n ");
                    choice = scanner.nextLine();
                    if(choice.equalsIgnoreCase("y"))
                    {
                        charName = scanner.nextLine();
                        Character character = new Character(charName);
                        characterTempList.add(character);
                        player.addCharacter(characterTempList);
                        gameToMake.addCharacters(player.get_charactersOwned().get(idx));
                        player.get_charactersOwned().get(idx).addGame(gameToMake);
                        break;
                    }else{break;}
                }
            }

修复这段代码一样诱人,我宁愿使用解决方法。

如果没有使用外部库,是否存在Java等效项?

1 个答案:

答案 0 :(得分:2)

不,Java中没有等价物。相反,如果你需要检查一个字符列表(每个都有一个名字)是否包含一个字符名称,那么只需这样做:

// search the name
boolean found = false;
for (Character c : player.get_charactersOwned()) {
    if (c.get_characterName().equals(charName)) {
        found = true;
        break;
    }
}

// perform the check
if (found) {
    // do something
} else {
    // do something else
}

顺便说一句,Character对你的班级来说是一个坏名字,它与Java自己的Character类发生了冲突。尽可能重命名,以避免混淆。或者,循环可以这样写:

boolean found = false;
for (int i = 0, n = player.get_charactersOwned().size(); i < n && !found; i++) {
    Character c = player.get_charactersOwned().get(i);
    if (c.get_characterName().equals(charName)) {
        found = true;
    }
}