在一个while循环中在两个嵌套的while循环之间切换

时间:2017-06-16 14:24:05

标签: java loops

我是一名新手程序员,我的作业包含while循环。

目前,我正在为一个游戏的战斗系统编程,该游戏有两个阶段需要在彼此之间循环(攻击和防御阶段)。它是作为一种方法创建的,我可以在游戏中多次调用它。我打算如何使代码工作是通过创建一个while循环来继续,直到其中一个actor的生命值达到0.接下来,我在第一个while循环中嵌套while循环以使用符合条件的booleans嵌套循环继续在两者之间切换。但是,在测试代码并尝试其他循环代码的方法之后,它似乎陷入了循环防御阶段并且没有切换回攻击阶段。

我真的很感激反馈并帮助阻止代码执行此操作。忽略"使用项目"我还没有添加它作为一个选择,所以它现在只是文本。

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");
}
}

1 个答案:

答案 0 :(得分:1)

defenceStance = true是一项任务,而非比较,因此while(defenceStance = true)始终为真,因为它正在测试分配是否成功。您需要使用==来比较值。但是因为你测试的是真的,你可以说while(defenceStance),因为bool == true总是会产生bool的值。