【发布时间】: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"))
{
};
}
}
}
【问题讨论】:
-
我知道您不想听到其他错误,但是虽然 (T = true) 每次都将 true 分配给 T,但您可能想要 while (T)
标签: java eclipse loops random while-loop