Java文本冒险游戏随机敌人选择异常:始终选中Assassin
问题描述
我正在开发一款基于文本的冒险游戏,但随机敌人选择功能无法正常工作:始终只有“Assassin”被选中。我尝试拆分随机选择步骤并打印生成的索引,发现索引是随机的,但显示的敌人始终是同一个。
主类代码
package defaultdfs; import java.util.Random; import java.util.Scanner; public class main { // System Objects static Scanner in = new Scanner(System.in); static Random rand = new Random(); //OBJ static Entity s = new Entity("Skeleton", 200, 5, 2, 200); static Entity z = new Entity("Zombie", 150, 2, 1, 150); static Entity w = new Entity("Warrior", 175, 3, 1, 175); static Entity a = new Entity("Assasin", 75, 1, 1, 75); // Game Variables static Entity[] enemies = {w, z, s, a}; static int maxEnemyHealth = 75; static int enemyAttackDam = 25; // Player Variables static int health = 100; static int attackDamage = 50; static int numHealthPot = 3; static int healthPotHealAmount = 30; static int healthPotDropChance = 50; static int ShieldDropChance = 0; static int enemyCounter = 0; static int defense = 0; static int enemyHealth = 0; static int damageDealt = 0; static int damageTaken = 0; //Items static boolean sword = false; static int swordDropChance = 0; static boolean running = true; static Entity enemy; public static void main(String[] args) { welcome(); GAME: while (running) { System.out.println("-------------------------------------------------"); int ra = rand.nextInt(enemies.length); //RANDOM ENEMY SELECTION in two steps enemy = enemies[ra]; // enemy = enemies[rand.nextInt(enemies.length)]; //in one step System.out.println(ra); System.out.println("\t# " + enemy.name + " appeared! #\n"); while (enemy.enemyHealth > 0) { monitoring(enemy, Entity.enemyHealth); String input = in.nextLine(); if (input.equals("1")) { damageDealt = rand.nextInt(attackDamage); damageTaken = rand.nextInt(enemyAttackDam); enemy.enemyHealth -= damageDealt; health -= damageTaken; // Iteminfluence health += defense; System.out.println("\t> You strike the " + enemy.name + " for " + damageDealt + " damage."); System.out.println("\t> You recive " + damageTaken + " in retaliation!"); if (health < 1) { dead(); break; } } else if (input.equals("2")) { healingProcess(enemy); } else if (input.equals("3")) { runOption(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; } defmsg(enemy); enemyC(); healpot(enemy); defense(enemy); sword(enemy); options(); String input = in.nextLine(); while (!input.equals("1") && !input.equals("2")) { System.out.println("Invalid command!"); input = in.nextLine(); } if (input.equals(input.equals("1"))) { System.out.println("You continue on ypur adventure!"); } else if (input.equals("2")) { System.out.println("You exit the dungeon, successful from your adventure!"); break; } } thx(); } private static void sword(Entity enemy) { if (rand.nextInt(100) < swordDropChance) { attackDamage += 5; System.out.println(" # The " + enemy.name + " dropped a piece of armor! # "); System.out.println(" # You attack now with +5! # "); } } private static void defmsg(Entity enemy) { System.out.println("-------------------------------------------------"); System.out.println(" # " + enemy.name + " was defeated! # "); System.out.println(" # You have " + health + "HP left. # "); enemy.enemyHealth = enemy.inHealth; } private static void options() { 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"); } private static void thx() { System.out.println("#######################"); System.out.println("# THANKS FOR PLAYING! #"); System.out.println("#######################"); } private static void welcome() { System.out.println("Welcome to the Dungeon!"); } private static void attack(int enemyHealth, int damageDealt, int damageTaken) { enemy.enemyHealth -= damageDealt; health -= damageTaken; // Iteminfluence health += defense; } private static void dead() { System.out.println("\t> You have taken too much damage, you are too weak to go on!"); } private static void monitoring(Entity enemy, int enemyHealth) { System.out.println("\tYour HP: " + health); System.out.println("\t" + enemy.name + "'s HP: " + enemy.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!"); } private static void runOption(Entity enemy) { System.out.println("\tYou run away from the " + enemy.name + "!"); } private static void healingProcess(Entity enemy) { if (numHealthPot > 0) { int prevHealth = health; health += healthPotHealAmount; if (health == 100) { System.out.println("You can not heal yourself!"); } else if (health > 100) { health = 100; numHealthPot--; System.out.println("\t> You drink a health potion, healing yourself for " + healthPotHealAmount + " . " + "\n\t> You now have " + health + " HP." + "\n\t> You have " + numHealthPot + " health potions left.\n"); } else { numHealthPot--; System.out.println("\t> You drink a health potion, healing yourself for " + healthPotHealAmount + " . " + "\n\t> You now have " + health + " HP." + "\n\t> You have " + numHealthPot + " health potions left.\n"); } } else { System.out.println("\t> You have no health potions left! Defeat enemies for a chance to get one!"); } } private static void enemyC() { enemyCounter++; if (enemyCounter == 1) { System.out.println(" # You killed one enemy! # "); } else { System.out.println(" # You killed " + enemyCounter + " enemies! # "); } } private static void healpot(Entity enemy) { if (rand.nextInt(100) < healthPotDropChance) { numHealthPot++; System.out.println(" # The " + enemy.name + " dropped a health potion! # "); System.out.println(" # You now have " + numHealthPot + " heaalth potion(s). # "); } } public static void defense(Entity enemy) { if (rand.nextInt(100) < ShieldDropChance) { defense += 5; System.out.println(" # The " + enemy.name + " dropped a piece of armor! # "); System.out.println(" # You now have " + defense + " defensepoints! # "); } } }
Entity类代码
public class Entity { static String name; static int enemyHealth = 100; static int ShieldDropChance = 0; static int swordDropChance = 0; static int inHealth = 0; public static int getSwordDropChance() { return swordDropChance; } public static void setSwordDropChance(int swordDropChance) { Entity.swordDropChance = swordDropChance; } public static int getShieldDropChance() { return ShieldDropChance; } public static void setShieldDropChance(int shieldDropChance) { ShieldDropChance = shieldDropChance; } public static String getName() { return name; } public static void setName(String name) { Entity.name = name; } public static int getEnemyHealth() { return enemyHealth; } public static void setEnemyHealth(int enemyHealth) { Entity.enemyHealth = enemyHealth; } public Entity (String name, int enemyHealth, int ShieldDropChance, int swordDropChance, int inHealth) { this.name = name; this.enemyHealth = enemyHealth; this.ShieldDropChance = ShieldDropChance; this.swordDropChance = swordDropChance; this.inHealth = inHealth; } }
问题原因及解决方法
核心问题
Entity类的所有字段都被声明为static,这意味着这些变量属于类本身,而非类的实例。当你依次创建Skeleton、Zombie、Warrior、Assassin四个Entity实例时,每个实例都会覆盖静态字段的值。最后创建的Assassin会把name、enemyHealth等静态变量全部替换成自己的属性,所以无论随机选中哪个数组索引,访问的都是类级别的静态变量,自然只会显示Assassin。
修复步骤
- 移除Entity类中所有字段的static修饰符,让每个实例拥有独立属性:
public class Entity { String name; int enemyHealth = 100; int ShieldDropChance = 0; int swordDropChance = 0; int inHealth = 0; // ... 其他代码 }
- 移除所有getter/setter方法的static修饰符,静态方法无法访问实例变量:
public int getSwordDropChance() { return swordDropChance; } public void setSwordDropChance(int swordDropChance) { this.swordDropChance = swordDropChance; } public int getShieldDropChance() { return ShieldDropChance; } public void setShieldDropChance(int shieldDropChance) { ShieldDropChance = shieldDropChance; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getEnemyHealth() { return enemyHealth; } public void setEnemyHealth(int enemyHealth) { this.enemyHealth = enemyHealth; }
- 修正主类中的逻辑错误:主循环的选项判断里,
input.equals(input.equals("1"))是错误的,应改为input.equals("1"):
if (input.equals("1")) { System.out.println("You continue on your adventure!"); }
完成以上修改后,每个Entity实例会保存自己的独立属性,随机选择敌人时就能正确显示对应敌人的信息了。
内容的提问来源于stack exchange,提问作者VAMPYR CITY
相关产品推荐
相关产品推荐

