【问题标题】:Java Math.random won't refresh on while loop (of course)Java Math.random 不会在 while 循环上刷新(当然)
【发布时间】:2016-02-25 18:03:27
【问题描述】:

所以,我正在尝试制作基于文本的 RPG。一个很好的入门项目,因为我对这一切都不熟悉。我想重击,math.random 似乎是最好的方法。只有一个问题;当它循环时,它不会刷新随机数。我可以假设那是因为它调用它一次,一旦它被调用,就是这样。这就是它的价值。我想知道一种替代方法,因此每次 while 循环重新开始时它都会刷新。我知道我的代码中还有其他一些错误;但这不是我现在关注的。这是整个方法:

    public void battleStart(Enemy enemy, Player player)
    {

        while (enemy.health != 0)
        {
            String battle = null;

            int criticalHt = (int) (Math.random() * 10) + 0;

            boolean T = true;
            while (T = true)
            {

                battle = Game.console.next();

                enemy.attackplayer(player);

                if (battle.toLowerCase().contains("skip"))
                {

                    System.out.println(criticalHt);

                    if (criticalHt == 1)
                    {

                        player.hitPoints -= enemy.dmg * 2;

                        System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + " for a critical level of " + enemy.dmg * 2 + "!.\n Your health is now " + player.hitPoints + ".");

                    }

                    else
                    {

                        player.hitPoints -= enemy.dmg;

                        System.out.println(Game.toTitleCase(enemy.firstName) + " " + Game.toTitleCase(enemy.lastName) + " has used " + Game.toTitleCase(enemy.attackName) + ".\n Your health is now " + player.hitPoints + ".");

                        System.out.println("Your turn to attack! Use an attack, or type \"skip\" to let your enemy strike.");
                    };
                    if (battle.contains("skp"))
                    {

                    };

                }
            }
        }

【问题讨论】:

标签: java eclipse loops random while-loop


【解决方案1】:

移动代码

 int criticalHt = (int)(Math.random() * 10) + 0;

在while循环中

while(T = true){
  int criticalHt = (int)(Math.random() * 10) + 0;
  battle = Game.console.next();
  ...

【讨论】:

  • 要么你应该治愈癌症,要么我的智商正好是 78。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多