我一直试图弄清楚如何制作它,以便玩家可以选择在最后重启游戏,但每当我尝试重新启动循环时,它会说
Label Game was not found
即使在此代码中清楚地显示了它。
// System objects
Scanner in = new Scanner(System.in);
Random rand = new Random();
// Game variables
String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin"};
int maxEnemyHealth = 75;
int enemyAttackDamage = 25;
int enemyDeaths = 0;
List scores = new ArrayList();
// Player variables
int health = 100;
int attackDamage = 50;
int numHealthPotions = 3;
int healthPotionHealAmount = 30;
int healthPotionDropChance = 50; // Percentage
boolean running = true;
System.out.println("Welcome to the Dungeon!");
Game:
while(running) {
System.out.println("-----------------------------------------------");
int enemyHealth = rand.nextInt(maxEnemyHealth);
String enemy = enemies[rand.nextInt(enemies.length)];
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");
String input = in.nextLine();
if(input.equals("1")) {
int damageDealt = rand.nextInt(attackDamage);
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 received " + 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(numHealthPotions > 0) {
health += healthPotionHealAmount;
numHealthPotions--;
System.out.println("\t> You drink a health potion, healing yourself for " + healthPotionHealAmount + "."
+ "\n\t> You now have " + health + " HP"
+ "\n\t> You have " + numHealthPotions + " health potions left.\n");
}
else {
System.out.println("\t> You have no health potions left! Defeat enemies for a chance to get one!");
}
}
else if(input.equals("3")) {
System.out.println("\tYou ran 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;
}
enemyDeaths++;
System.out.println("-----------------------------------------------");
System.out.println(" # " + enemy + " was defeated! #");
System.out.println(" # You have " + health + " HP left. #");
System.out.println(" # Your current score is " + enemyDeaths * 100 + " # ");
if(rand.nextInt(100) < healthPotionDropChance) {
numHealthPotions++;
System.out.println(" # The " + enemy + " dropped a health potion! #");
System.out.println(" # You have " + numHealthPotions + " 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");
String input = in.nextLine();
while(!input.equals("1") && !input.equals("2")) {
System.out.println("Invalid Command");
input = in.nextLine();
}
if (input.equals("1")) {
System.out.println("You continue on your adventure!");
}
else if (input.equals("2")) {
System.out.println("You exit the dungeon, successful from your adventures!");
scores.add(enemyDeaths * 100);
break;
}
}
System.out.println("######################");
System.out.println("# THANKS FOR PLAYING #");
System.out.println("######################");
String randomWords;
in = new Scanner(System.in);
System.out.println("Enter a name to be remembered by");
randomWords = in.next();
scores.add(randomWords + " " + enemyDeaths * 100);
System.out.println(scores);
System.out.println("\n");
System.out.println("\n");
{
System.out.println("Would you like to play again?");
String input = in.nextLine();
if(input.equals("yes")) {
continue Game;
}
}
}
}
答案 0 :(得分:0)
if(input.equals("no")) running = false;
这样,while循环就会干净利落,你不需要使用繁琐的标签来控制流量。
答案 1 :(得分:0)
为了简单起见,您只需更改退出条件:
if (!input.equals("yes")) {
break; // end the loop
}
只是一个提示:将文字与某个变量进行比较被认为是一种很好的做法:
if (!"yes".equals(input)) {
break; // end the loop
}
如果NullPointerException
为input
,则以null
进行检查不会失败,在这种情况下只会返回false
。
答案 2 :(得分:0)