简体   繁体   中英

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. 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?

/**
* 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. 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.

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:

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. I think you need only one Do..While Loop that loops until either charHp or enemyHp is zero.

        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 {

               //.... 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. But the the code will still be executed each time as the outer loops continue to loop until a score drops to 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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