运行程序时偶尔突然停止

时间:2017-02-16 01:08:35

标签: java if-statement do-while

我正在尝试创建一个口袋妖怪之间的大型战斗的模拟。为了提供一个例子,我将使用两个Pokemon,名为pokemon1和pokemon2。我希望程序像以下一样工作:

  • pokemon1和pokemon2将根据他们的combatScore进行比较,这是一个基于他们的基本属性的分数,如速度,攻击,防御等。
  • pokemon1和pokemon2将随机配对并进行比较。
  • 如果pokemon1的战斗力比pokemon2高,那么pokemon1会进入下一轮(并从一般人群中移除并被置于“胜利”人群中),pokemon2将从一般人群中删除并添加到“被征服的“人口。
  • pokemon1然后根据pokemon2的特征获得一些属性。
  • 如果pokemon2(被击败)具有传奇色彩,他们必须总共失去三次才能最终加入被征服的人口。
  • 如果pokemon1(胜利)不具有传奇色彩,但已赢了三次,那么它就会变得具有传奇色彩,因此在被加入被征服的人群之前必须失去三次。

在没有越来越少的胜利人口的情况下重复这些轮次,直到只剩下一名胜利者为止。

我创建了一个do-while循环,它将从群体中随机检索两个Pokemon,并使用if-else分支根据许多不同的条件对它们进行比较:

  1. 传奇的pokemon2击败传奇的pokemon1
  2. pokemon2 beats legendary pokemon1
  3. 传奇pokemon2 beats pokemon1
  4. pokemon2 beats pokemon1
  5. 反之亦然。我遇到的问题是:大多数时候,程序运行干净,第1轮完成。所有的战斗都写在一个pokemon.txt文件中,明确说明谁赢了。但是,有时程序将开始运行并保持运行状态。当我去检查.txt文件时,它将停止在同一位置(第275行)周围写句子。

    现在,我只想进行第一轮比赛。以下是运行此模拟器的代码:

      do {
            Pokemon pokemon1;
            Pokemon pokemon2;
            pokemon1 = population.get((int) (Math.random() * population.size()));
            do {
                pokemon2 = population.get((int) (Math.random() * population.size()));
            } while (pokemon1.getId() == pokemon2.getId());
    
    
            if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
                    && pokemon2.isLegendary() && pokemon1.isLegendary()) {
                bufferedWriter.write("The legendary " + pokemon2.getName() + " defeats the legendary " +
                        pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon2.incAttack(pokemon1.getCombatScore());
                pokemon2.incDefense(pokemon1.getCombatScore());
                pokemon2.addVict();
    
                victorious.add(pokemon2);
                population.remove(pokemon2);
    
                pokemon1.addLoss();
    
                if (pokemon1.numLoss == 3) {
                    defeated.add(pokemon1);
                    population.remove(pokemon1);
                }
            }
    
            else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
                    && pokemon1.isLegendary()) {
                bufferedWriter.write(pokemon2.getName() + " defeats the legendary " +
                        pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon2.incAttack(pokemon1.getCombatScore());
                pokemon2.incDefense(pokemon1.getCombatScore());
    
                pokemon2.addVict();
                if (pokemon2.numVict == 3) {
                    bufferedWriter.write(pokemon2.getName() + " has become legendary!");
                }
    
                victorious.add(pokemon2);
                population.remove(pokemon2);
    
                pokemon1.addLoss();
                if (pokemon1.numLoss == 3) {
                    defeated.add(pokemon1);
                    population.remove(pokemon1);
                }
            }
    
            else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
                    && pokemon2.isLegendary()) {
                bufferedWriter.write("The legendary " + pokemon2.getName() + " defeats " +
                        pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon2.incAttack(pokemon1.getCombatScore());
                pokemon2.incDefense(pokemon1.getCombatScore());
                pokemon2.addVict();
    
                victorious.add(pokemon2);
                population.remove(pokemon2);
    
                pokemon1.addLoss();
    
                defeated.add(pokemon1);
                population.remove(pokemon1);
            }
            else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()) {
                bufferedWriter.write(pokemon2.getName() + " defeats " +
                        pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon2.incAttack(pokemon1.getCombatScore());
                pokemon2.incDefense(pokemon1.getCombatScore());
    
                pokemon2.addVict();
                if (pokemon2.numVict == 3) {
                    bufferedWriter.write(pokemon2.getName() + " has become legendary!");
                }
    
                victorious.add(pokemon2);
                population.remove(pokemon2);
    
                pokemon1.addLoss();
    
                defeated.add(pokemon1);
                population.remove(pokemon1);
            }
            else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
                    && pokemon2.isLegendary() && pokemon1.isLegendary()) {
                bufferedWriter.write("The legendary " + pokemon1.getName() + " defeats the legendary " +
                        pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon1.incAttack(pokemon2.getCombatScore());
                pokemon1.incDefense(pokemon2.getCombatScore());
                pokemon1.addVict();
    
                victorious.add(pokemon1);
                population.remove(pokemon1);
    
                pokemon2.addLoss();
    
                if (pokemon2.numLoss == 3) {
                    defeated.add(pokemon2);
                    population.remove(pokemon2);
                }
            }
    
            else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
                    && pokemon2.isLegendary()) {
                bufferedWriter.write(pokemon1.getName() + " defeats the legendary " +
                        pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon1.incAttack(pokemon2.getCombatScore());
                pokemon1.incDefense(pokemon2.getCombatScore());
    
                pokemon1.addVict();
                if (pokemon1.numVict == 3) {
                    bufferedWriter.write(pokemon1.getName() + " has become legendary!");
                }
    
                victorious.add(pokemon1);
                population.remove(pokemon1);
    
                pokemon2.addLoss();
                if (pokemon2.numLoss == 3) {
                    defeated.add(pokemon2);
                    population.remove(pokemon2);
                }
            }
    
            else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
                    && pokemon1.isLegendary()) {
                bufferedWriter.write("The legendary " + pokemon1.getName() + " defeats " +
                        pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon1.incAttack(pokemon2.getCombatScore());
                pokemon1.incDefense(pokemon2.getCombatScore());
                pokemon1.addVict();
    
                victorious.add(pokemon1);
                population.remove(pokemon1);
    
                pokemon2.addLoss();
    
                defeated.add(pokemon2);
                population.remove(pokemon2);
            }
            else {
                bufferedWriter.write(pokemon1.getName() + " defeats " +
                        pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
                bufferedWriter.newLine();
    
                /* pokemon2 gains xp from winning */
                pokemon1.incAttack(pokemon2.getCombatScore());
                pokemon1.incDefense(pokemon2.getCombatScore());
    
                pokemon1.addVict();
                if (pokemon1.numVict == 3) {
                    bufferedWriter.write(pokemon1.getName() + " has become legendary!");
                }
    
                victorious.add(pokemon1);
                population.remove(pokemon1);
    
                pokemon2.addLoss();
    
                defeated.add(pokemon2);
                population.remove(pokemon2);
            }
    
        }while (population.size() != 0);
    

    以下是.txt文件“永远”运行时的示例屏幕截图:

    272 Stoutland defeats Loudred with a combat score of 5415!
    273 Croconaw defeats Electrike with a combat score of 3272!
    274 Rapidash defeats Meditite with a combat score of 7390!
    275 Jynx defeat
    

    我已经尝试调试并逐步完成每个条件以确保它们正常工作。我很有信心他们都这样做。问题出在其他地方吗?当人口中存在奇怪数量的口袋妖怪时,它是否会遇到问题?任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题在于,当为战斗选择两个竞争者时,你进入一个永不退出的循环:

pokemon1 = population.get((int) (Math.random() * population.size()));
do {
    pokemon2 = population.get((int) (Math.random() * population.size()));
} while (pokemon1.getId() == pokemon2.getId());

这看起来很好,但是如果人口只有一个口袋妖怪会怎么样?它永远不会选择第二个并且永远不会退出循环。

至于为什么它停止写中句,你正在使用缓冲的编写器,它不一定立即将它的内容刷新到文件中。当你陷入循环时,编写器正在等待更多信息写入,并将其余部分保留在缓冲区中。对flush()的调用可以帮助你解决这个问题,但确保有足够的口袋妖怪进行战斗是一个更大的问题。