0
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
我正在做一个基于文本的冒险游戏,为我的机器人俱乐部练习java,我的代码不断重复后,它说明了敌人,它重复上面的东西在控制台。整个代码在,它可能是一个简单的语法错误,但我找不到它在哪里或什么是错的。代码重复,当它不应该
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("######################");
}
}
一旦你解决了你的问题,考虑将它发布到Code Review。你在'main()'方法中试图做得太多。 – jsheeran