简体   繁体   English

我的do-while陈述似乎无效

[英]My do-while statement doesn't seem to work

I'm completely new to coding and I was wondering if you guys can help me. 我是编码的新手,我想知道你们是否可以帮助我。 This is part of a code in which you battle a vampire, RPG style. 这是与RPG风格的吸血鬼作斗争的代码的一部分。 My program does not loop back to my characters turn after I set the turn number to one near the bottom of the code, why is this? 当我在代码底部附近将转数设置为1后,我的程序不会循环回到我的字符转,这是为什么?

/**
* Created by f on 7/30/2014.
*/
import java.util.Scanner;
import java.util.Random;

public class rpgBattle {
public static void main(String[] args) {
    // Declarations
    int charHp = 3941;
    int enemyHp = 5200;
    String charName;
    int numDmg;
    int dmgMultiplier = 1;
    String playerInputSt;
    int playerInput;
    int turn = 1;
    int miss;

    //Processes
    Scanner user_input = new Scanner(System.in);
    System.out.println("Enter your name.");
    charName = user_input.next();
    System.out.println();
    System.out.println("A vampire emerged!");
    do {
                System.out.println();
                System.out.println(charName + "'s HP: " + charHp + "/3941");
                System.out.println("The Vampire's HP: " + enemyHp + "/5200");
                System.out.println("What will you do?");
                System.out.println("Enter the number corresponding to the action you would like to perform.");
                System.out.println("1. Attack");
                System.out.println("2. Defend");
                System.out.println(turn);
                playerInputSt = user_input.next();
                playerInput = Integer.parseInt(playerInputSt);
                if (playerInput == 1) {
                    Random rand = new Random();
                    miss = rand.nextInt(19);
                    if (miss == 0) {
                        System.out.println();
                        System.out.println("The Vampire protected itself.");
                        numDmg = 0;
                    } else {
                        numDmg = rand.nextInt(100) + 550;
                    }
                    enemyHp = enemyHp - numDmg / 1;
                    System.out.println();
                    System.out.println(charName + " attacks!");
                    System.out.println("The Vampire took " + numDmg / dmgMultiplier + " damage!");
                    turn = 2;
                } else if (playerInput == 2) {
                    System.out.println();
                    System.out.println(charName + " guards");
                    System.out.println(charName + " recovered 394 HP!");
                    charHp = charHp + 394;
                    dmgMultiplier = 2;
                    turn = 2;
                };
    } while (charHp > 0 && enemyHp > 0 && turn !=2);
    do {
        Random rand = new Random();
        miss = rand.nextInt(19);
        if (miss == 0) {
            System.out.println();
            System.out.println(charName + " braced himself.");
            numDmg = 0;
        } else {
            numDmg = rand.nextInt(500) + 200;

            charHp = charHp - numDmg / dmgMultiplier;
        }
        System.out.println("The Vampire attacks!");
        System.out.println(charName + " took " + numDmg / dmgMultiplier + " damage!");
        dmgMultiplier = 1;
        turn = 1;
    } while (turn == 2);
}
}

It will never end because in the top level loop, the condition to get out is that the character's health drops below 0. However, you are never decreasing his HP. 它永远不会结束,因为在顶级循环中,退出的条件是角色的生命值降至0以下。但是,您绝不会降低其HP。 You are only decreasing the vampire's HP, but not the character. 您只是在降低吸血鬼的生命值,而不是角色。 If you want the game to end, make the vampire attack the character too. 如果您希望游戏结束,请让吸血鬼也攻击角色。 That way, in some point his HP will drop below 0 and the game will end. 这样,在某些时候他的HP将降至0以下,游戏将结束。

However, to make it more realistic, you should make the game end when the HP of the vampire OR the character are 0, and not when both of them are below 0. To achieve that, instead of using 3 loops, use 2, but changing the condition so that when either one of them has no HP, it will end: 但是,要使其更加逼真,您应该在吸血鬼的HP或角色的HP为0时结束游戏,而不是在两者都低于0时结束游戏。要实现这一点,请使用2,而不是使用3个循环更改条件,以便当其中任何一个没有HP时,它将结束:

do {
    do {
        ...
    } while (turn == 1);
} while (charHp > 0 && enemyHp > 0);

Without spending too much time analyzing all of your code, it looks like you have nested a Do..While Loop for charHp and a Do..While Loop for enemyHp. 无需花费太多时间分析所有代码,就好像您为charHp嵌套了Do..While循环,而对敌人Hp嵌套了Do..While循环。 I think you need only one Do..While Loop that loops until either charHp or enemyHp is zero. 我想你只需要一个do..while循环是循环, 直到 charHp或enemyHp为零。

        do {
            do {

               //.... Lots of code removed for brevity in the answer .....

                } while (turn == 1);
        } while ((enemyHp > 0) && (charHp > 0));

or should it be like this, with only one Do..While Loop 还是应该这样,只有一个Do..While循环

        do {

               //.... Lots of code removed for brevity in the answer .....

        } while ((turn == 1) && (enemyHp > 0) && (charHp > 0));

What is the purpose of the variable turn ? 可变转弯的目的是什么? Once a valid value is entered its value will be 2 untill the end of the game so the inner while will always loop only once. 输入有效值后,直到游戏结束,其值将一直为2,因此内部while循环将始终仅循环一次。 But the the code will still be executed each time as the outer loops continue to loop until a score drops to 0. 但是随着外部循环继续循环直到得分降为0,代码仍将每次执行。

If you want to know the number of turns you are playing, you should increase turn: 如果您想知道正在玩的回合数,则应增加转数:

turn++;

and remove the most inner while loop. 并删除最内部的while循环。

If you want to loop until the user has entered a valid value, you should initialise turn at the beginning of the most inner loop: 如果要在用户输入有效值之前进行循环,则应在最内部循环的开始处初始化转弯:

    turn=1;

good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM