简体   繁体   English

代码在不应该重复时重复

[英]Code Repeats when its not supposed to

Welcome to the dungeon!
Type Start to begin
Start
Start
--------------------------------------
# Gang Member has appeared! #

Your HP: 200
Gang Member's HP: 75

What would you like to do?
1. Attack
2. Drink health potion
3. Run
Invalid command
Your HP: 200
Gang Member's HP: 75

What would you like to do?
1. Attack
2. Drink health potion
3. Run
Invalid command
Your HP: 200
Gang Member's HP: 75

I'm making a text based adventure game to practice java for my robotics club, and my code keeps repeating after it states the enemy and it repeats whats above in the console. 我正在为我的机器人俱乐部制作一个基于文本的冒险游戏,练习Java,并且在指出敌人并重复控制台中的内容后,我的代码不断重复。 The whole code is under, it may be a simple syntax error but i can't find where or whats wrong with it. 整个代码都在下面,这可能是一个简单的语法错误,但我找不到它的错误之处。

package idk;
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
/**
*
* @author Jack
 */
public class Idk{

/**
 *
 * @param args
 */
public static void main(String[] args) {

    // System objects
    Scanner in = new Scanner(System.in);
    Random rand = new Random();


    // Player variables
    int health = 200;
    int attackDmg = 50;
    int numHealthPots = 3;
    int healthPotionHealAmount = 45;
            //String enemy = [rand.nextInt(idk.enemies.enems.length
            boolean running = false;

    System.out.println("Welcome to the dungeon!");
            System.out.println("Type Start to begin");
            String input = in.nextLine();
            if(input.equals("Start") ){
                running = true;
            }



    // Label
            GAME:

    while (running) {                       
                int enemyNumber = rand.nextInt(6);
                int enemyHealth;
                int enemyAttackDamage;
                int healthPotionDropChance;
                String enemy /*= "no enemy"*/;        
            switch(enemyNumber){   
                case 0:
                   enemy = "Alien";
                   enemyHealth =75;
                   enemyAttackDamage = 25;
                   healthPotionDropChance = 35; 
                   break;

                case 1:
                   enemy = "Thief";
                   enemyHealth = 90;
                   enemyAttackDamage = 30;
                   healthPotionDropChance = 45; 
                   break;
                case 2:
                    enemy = "Ravager";
                    enemyHealth = 100;
                    enemyAttackDamage = 36;
                    healthPotionDropChance = 45;
                    break;
                case 3:
                    enemy = "Gang Boss";
                    enemyHealth = 150;
                    enemyAttackDamage = 45;
                    healthPotionDropChance = 45;
                    break;
                case 4:
                    enemy = "Gang Member";
                    enemyHealth = 75;
                    enemyAttackDamage = 55;
                    healthPotionDropChance = 35;
                    break;
                case 5:
                    enemy = "Mercenary";
                    enemyHealth = 250;
                    enemyAttackDamage = 18;
                    healthPotionDropChance = 25;
                    break;
                case 6:
                    enemy = "Gang Boss With His Crew";
                    enemyHealth = 205;
                    enemyAttackDamage = 25;
                    healthPotionDropChance = 65 ;
                    break;
                default:
                    enemy = "no enemy showed up";
                    enemyHealth = 1;
                    enemyAttackDamage = 1;
                    healthPotionDropChance = 1 ;
                    break;
                }
                    input = in.nextLine();
                    System.out.println("--------------------------------------");                       
        System.out.println("\t# " + enemy + " has appeared! #\n");

        while (enemyHealth > 0) {
            System.out.println("\tYour HP: " + health);
            System.out.println("\t" + enemy + "'s HP: " + enemyHealth);
            System.out.println("\n\tWhat would you like to do?");
            System.out.println("\t1. Attack");
            System.out.println("\t2. Drink health potion");
            System.out.println("\t3. Run");
            if (input.equals("1")) {
                int damageDealt = rand.nextInt(attackDmg);
                int damageTaken = rand.nextInt(enemyAttackDamage);

                enemyHealth -= damageDealt;
                health -= damageTaken;

                System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage");
                System.out.println("\t> You recieved " + damageTaken + " in retaliation");

                if (health < 1) {
                    System.out.println("\t You have taken too much damage, you are too weak to go on");
                    break;
                }
            } else if (input.equals("2")) {

                if (numHealthPots > 0) {
                    health += healthPotionHealAmount;
                    numHealthPots--;
                    System.out.println("\t> You drank a health potion, healed for: " + healthPotionHealAmount + "."
                            + "\n\t> You now have" + health + "HP."
                            + "\n\t> You now have" + numHealthPots + " health potions left.\n");
                } else {
                    System.out.println("\t> You have no health potions, defeat enemies for a chance to get one");
                }

            } else if (input.equals("3")) {
                System.out.println("\t> You run away from the " + enemy);
                continue GAME;
            } else {
                System.out.println("\tInvalid command");
            }
        }
        if (health < 1) {
            System.out.println("You limp out of the dungeon, weak from battle.");
            break;
        }
        System.out.println("--------------------------------------");
        System.out.println(" # " + enemy + " was defeated! # ");
        System.out.println(" # You have " + health + "HP left # ");
        // If the random number is less than 50 it drops
        if (rand.nextInt(100) < healthPotionDropChance) {
            numHealthPots++;
            System.out.println(" # The " + enemy + " dropped a health potion. # ");
            System.out.println(" # You now have " + numHealthPots + " health potion(s). # ");
        }
        System.out.println("--------------------------------------");
        System.out.println("What would you like to do now?");
        System.out.println("1. Continue fighting");
        System.out.println("2. Exit dungeon");

        while (!input.equals("1") && !input.equals("2")) {
            System.out.println("invalid command");
            input = in.nextLine();

        }
        if (input.equals("1")) {
            System.out.println("You continue your adventure.");
        } else if (input.equals("2")) {
            System.out.println("You exit the dungeon.");
            break;
        }
    }
    System.out.println("######################");
    System.out.println("# THANKS FOR PLAYING #");
    System.out.println("######################");
}
}

兄弟,您在向用户询问选项后忘记了input.nextInt()

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

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