首页 > 解决方案 > 某些包含打印行的“if”语句未执行,因此未打印到控制台

问题描述

我正在编写一个基于文本的 Java 游戏,用户在其中输入某些输入以最终击败敌人。我想通过写下 if 语句来创建分数,该语句说明对敌人造成的伤害是否为 x 量,然后奖励 x 分。但是,我相信 if 语句被跳过了,因为它没有打印 print 语句。

import java.util.Scanner;
import java.util.Random;



public class Main {

    public static void main(String[] args) {

        game gameObject = new game();
        gameObject.runGame();
    }
}


class game {

    public void runGame() {
        // System Objects
        Scanner in = new Scanner(System.in);
        Random rand = new Random();


        // Game Variables 
        String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assasain"};

        int maxEnemyHealth = 100;
        int enemyAttackDamage = 50;


        // Player Variables 

        int health = 100;
        int attackDamage = 50;
        int numHealthPotions = 3;
        int healthPotionHealAmount = 30;
        int healthPotionDropChance = 50; // Percentage 
        int counter = 0;
        int points =0; 

        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);
                        System.out.println("\t> You recieve " + damageTaken + " in retaliation!");

                        if (health < 1) {
                            System.out.println("\t> You have taken too much damage, you are too weak to move on!");
                            break;
                        }

                        if (damageDealt > 20) {
                            points += 50;
                            System.out.print("You gained 50 points. You have" + points + " points in total.");
                        }

                        else if (damageDealt > 30) {
                            points += 60;
                            System.out.print("You gained 60 points. You have" + points + " points in total.");
                        }

                        else if (damageDealt >50) {
                            points += 100;
                            System.out.print("You gained 100 points. You have" + points + " points in total.");
                        }
                    }

                    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 potionsleft.\n");
                        }
                        else {
                            System.out.println("\t> You have no health potions left! Defeat enemies to get a chance to get one.\n");
                        }
                    }

                    else if (input.equals("3")) {

                        System.out.println("\tYou 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 (rand.nextInt(100) < healthPotionDropChance) {
                    numHealthPotions++;
                    System.out.println(" # The " + enemy + " dropped a health potion! # ");
                    System.out.println(" # You now 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("1. Exit the dungeon");

                String input = in.nextLine();

                while (!input.equals("1") && !input.equals("2")) {
                    System.out.print("Invalid command!");
                    input = in.nextLine();
                }

                if (input.contentEquals("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!");
                    break;
                }

                counter++;
            }
}

class exit {

    public String byeStatements () {
        String format;
        String thanks;
        format = "###########";
        thanks = "Thanks for playing!";

        System.out.println(format);
        System.out.println(thanks);

        return (format + thanks);
    }
}

标签: javaprintingsystem.out

解决方案


推荐阅读