2015-09-06 56 views
0

我一直在想如何讓玩家可以選擇在最後重新開始遊戲,但每當我嘗試重新啓動遊戲時,它都會說「找不到標籤」,儘管在Java源代碼中定義了

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

'}'的數量並不真正匹配'{' 閱讀新用戶的幫助,特別是,提供**最小**可驗證的示例。作爲一個暗示,把問題解決到你需要做的最少的事情來重新創建它,幫助我們自己解決它。 –

+1

這條線是問題:'繼續遊戲;'。 「繼續」是一個保留關鍵詞。查看Oracle教程以瞭解其含義:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html – Tunaki

回答

0

它更容易,如果你設置正好相反:

if(input.equals("no")) running = false; 

這樣while循環失控乾淨,你不需要使用繁瑣的標籤來控制你的流量。

+0

我認爲您想使用「no」 - 使用'no'不會編譯。 –

0

爲了簡單起見,你可以只改變退出標準:

if (!input.equals("yes")) { 
    break; // end the loop 
} 

只是一個提示:它被認爲是一個很好的做法,比較文字對一些變量:

if (!"yes".equals(input)) { 
    break; // end the loop 
} 

做如果inputnull,則這種方式的檢查將不會以NullPointerException失敗,在這種情況下它只返回false

0

Branching Statements

continue語句跳過的當前迭代,while或do-while循環。

您想從標籤while循環外部跳轉到標籤'Game', 這是不允許的。

相關問題