gpt4 book ai didi

java - 在一个 while 循环内的两个嵌套 while 循环之间切换

转载 作者:行者123 更新时间:2023-12-02 12:51:28 25 4
gpt4 key购买 nike

我是一名菜鸟程序员,我的包含 while 循环的作业存在问题。

目前,我正在编写一款游戏的战斗系统,该游戏有两个需要相互循环的阶段(攻击阶段和防御阶段)。它是作为一种方法创建的,我可以在游戏中多次调用该方法。我想让代码工作的方式是创建一个 while 循环,该循环将持续到其中一个 Actor 的生命值达到 0。接下来,我将 while 循环嵌套在第一个 while 循环中,以使用满足嵌套循环条件的 boolean 值来继续在两者之间切换。然而,在测试代码并尝试其他循环代码的方法之后,它似乎陷入了循环防御阶段而不是切换回攻击阶段的困境。

我非常感谢反馈并帮助防止代码这样做。也忽略代码中的“使用项目”位,我还没有将其添加为选项,所以现在它只是文本。

import java.util.*;

public class CombatSystemTesting
{
// Combat Variables
public static boolean attackStance = true;
public static boolean defenceStance = false;

// Player Variables
public static int playerGold = 0;
public static int playerHP = 20;
public static int playerArmour = 0;
public static int playerShield = 1;
public static int playerAttack = 3;

// Enemy Variables
public static int enemyHP = 12;
public static int enemyDF = 0;
public static int enemyAttack = 3;

public static void main(String[] args)
{
battle1();
}

public static void battle1()
{
int enemyAttackChance = (int)(Math.random()*3)+1;
int enemyDefendChance = (int)(Math.random()*3)+1;

Scanner sc = new Scanner(System.in);

while (playerHP <= 0 || enemyHP <= 0);
{
while (attackStance = true)
{
System.out.println("(Attack Phase)");
System.out.println("");
System.out.println("'1' - [Attack]");
System.out.println("'2' - [Strike]");
System.out.println("'3' - [Use Item]");
System.out.println("");
System.out.println("HP: " + playerHP + " DF: " + playerArmour + " Gold: " + playerGold);
System.out.println("");
System.out.println("Enemy HP: " + enemyHP + " Enemy DF: " + enemyDF);
System.out.println("");
System.out.print("Your choice: ");
int choice = sc.nextInt();

System.out.println("");
System.out.println("----------------------------------------------------------------------------------------------");
System.out.println("");

if (choice == 1)
{
if (enemyDefendChance == 1 || enemyDefendChance == 2)
{
System.out.println("You used: Attack!");
System.out.println("Rat Used: Defend!");

enemyDF = enemyDF + 1;
playerAttack = playerAttack - enemyDF;
enemyHP = enemyHP - playerAttack;
playerAttack = playerAttack + enemyDF;
enemyDF = enemyDF - 1;

attackStance = false;
defenceStance = true;
}
else if (enemyDefendChance == 3)
{
System.out.println("You used: Attack!");
System.out.println("Rat Used: Counter!");
System.out.println("");

playerAttack = playerAttack - enemyDF;
enemyHP = enemyHP - playerAttack;
playerAttack = playerAttack + enemyDF;

attackStance = false;
defenceStance = true;
}
}
if (choice == 2)
{
if (enemyDefendChance == 1 || enemyDefendChance == 2)
{
System.out.println("You used: Strike!");
System.out.println("Rat Used: Defend!");
System.out.println("");

playerAttack = playerAttack * 2;
enemyDF = enemyDF + 1;
enemyHP = enemyHP - playerAttack + enemyDF;
enemyDF = enemyDF - 1;
playerAttack = playerAttack / 2;

attackStance = false;
defenceStance = true;
}
else if (enemyDefendChance == 3)
{
System.out.println("You used: Strike!");
System.out.println("Rat Used: Counter!");
System.out.println("");

playerHP = playerHP - playerAttack * 2 + playerArmour;

attackStance = false;
defenceStance = true;
}
}
while (defenceStance = true)
{
System.out.println("(Defence Phase)");
System.out.println("");
System.out.println("'1' - [Defend]");
System.out.println("'2' - [Counter]");
System.out.println("'3' - [Use Item]");
System.out.println("");
System.out.println("HP: " + playerHP + " DF: " + playerArmour + " Gold: " + playerGold);
System.out.println("");
System.out.println("Enemy HP: " + enemyHP + " Enemy DF: " + enemyDF);
System.out.println("");
System.out.print("Your choice: ");
choice = sc.nextInt();

System.out.println("");
System.out.println("----------------------------------------------------------------------------------------------");
System.out.println("");

if (choice == 1)
{
if (enemyAttackChance == 1 || enemyAttackChance == 2)
{
System.out.println("You used: Defend!");
System.out.println("Rat Used: Attack!");

playerArmour = playerArmour + playerShield;
enemyAttack = enemyAttack - playerArmour;
playerHP = playerHP - enemyAttack;
enemyAttack = enemyAttack + playerArmour;
playerArmour = playerArmour - playerShield;
System.out.println("");

attackStance = true;
defenceStance = false;
}
else if (enemyAttackChance == 3)
{
System.out.println("You used: Defend!");
System.out.println("Rat Used: Strike!");

enemyAttack = enemyAttack * 2;
playerArmour = playerArmour + playerShield;
playerHP = playerHP - enemyAttack + playerArmour;
playerArmour = playerArmour - playerShield;
enemyAttack = enemyAttack / 2;
System.out.println("");

attackStance = true;
defenceStance = false;
}
}
else if (choice == 2)
{
if (enemyAttackChance == 1 || enemyAttackChance == 2)
{
System.out.println("You used: Counter!");
System.out.println("Rat Used: Attack!");

enemyAttack = enemyAttack - playerArmour;
playerHP = playerHP - enemyAttack;
enemyAttack = enemyAttack + playerArmour;

attackStance = true;
defenceStance = false;
}
else if (enemyAttackChance == 3)
{
System.out.println("You used: Attack!");
System.out.println("Rat Used: Counter!");
System.out.println("");

playerAttack = playerAttack - enemyDF;
enemyHP = enemyHP - playerAttack;
playerAttack = playerAttack + enemyDF;

attackStance = true;
defenceStance = false;
}
}
}
}
}
//Pending code will be added later
System.out.println("You shouldn't see this message till you or oppenent have reached 0 HP");
}
}

最佳答案

defenceStance = true 是一个赋值,而不是比较,因此 while(defenceStance = true) 将始终为 true,因为它正在测试赋值是否成功。您需要使用 == 来比较值。但由于您正在测试 true,因此您可以只说 while(defenceStance),因为 bool == true 将始终只得出 bool 的值。

关于java - 在一个 while 循环内的两个嵌套 while 循环之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44591700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com