java - 我的Java程序已编译但无法运行

标签 java compiler-errors

我的Java程序可以编译,但在尝试运行时不返回任何内容。我正在使用多种方法进行分配,因此不确定是否对方法造成了问题,这些问题可能会对根本无法运行的程序产生影响。

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

public class CombatCalculatorExpansion1{

    public static void main(String[] args){

        int monstersHealth = 100;
        int monsterAttackpower = 0;
        String monstersName;
        int xp;
        int yourHealth = 0;
        int yourAttackpower = 0;
        int magicPower = 0;
        int magicPoints = 0;
        int statPoints = 20;
    }
    public static void createHero(){

        int yourHealth = 0;
        int yourAttackpower = 0;
        int magicPower = 0;
        int magicPoints = 0;
        int statPoints = 20;    

        boolean loopControl = true;
            while (loopControl){


         System.out.println("***************************");
         System.out.println("Health: " + yourHealth);
         System.out.println("Attack: " + yourAttackpower);
         System.out.println("Magic: " + magicPower);
         System.out.println("***************************");
         System.out.println("5.) +10 Health");
         System.out.println("6.) +1 Attack");
         System.out.println("7.) +3 Magic");
         System.out.println("You have 20 points to spend: " + statPoints);
         System.out.println("***************************");

            int choice;
            Scanner inputReader = new Scanner(System.in);
            choice = inputReader.nextInt();

            if(choice == 5){
                yourHealth = yourHealth + 10;
                statPoints = statPoints - 1;
            } else if(choice == 6){
                yourAttackpower = yourAttackpower + 1;
                statPoints = statPoints - 1;
            } else if(choice == 7){
                magicPower = magicPower + 3;
                statPoints = statPoints - 1;
            } else {
                System.out.println("I don't understand that command.");
            } if(statPoints <= 0){
                loopControl = false;
                System.out.println("You don't have any stat points left to spend.");
            }
        }
    }

    public static void runCombat(){

        int monstersHealth = 100;
        int monsterAttackpower = 0;
        String monstersName;
        int xp;
        int yourHealth = 0;
        int yourAttackpower = 0;
        int magicPower = 0;
        int magicPoints = 0;
        int statPoints = 20;

        boolean loopControl = true;
            while (loopControl){

            System.out.println("Combat Options");

            System.out.println("1.) Sword Attack");
            System.out.println("2.) Cast Spell");
            System.out.println("3.) Charge Mana");
            System.out.println("4.) Run Away");
            System.out.println("What Action do you want to perform?");

            int choice;
            Scanner inputReader = new Scanner(System.in);
            choice = inputReader.nextInt();

            if(choice == 1){
                monstersHealth = monstersHealth - yourAttackpower;
                System.out.println("You strike the goblin with your sword for 12 damage!");
                System.out.println(monstersHealth + " health remaining");
            } else if (choice == 2){
                if(magicPower >= 3){ 
                System.out.println("You cast the weaken spell on the monster.");
                monstersHealth = monstersHealth / 2;
                } else
                System.out.println("You don't have enough mana.");
                System.out.println(monstersHealth + " health remaining");
            } else if (choice == 3){
                magicPower = magicPower + 1;
                System.out.println("You focus and charge your magic power.");

            } else if (choice == 4){

                loopControl = false;
                System.out.println("You run away!");
            } else if (choice >= 8){
                System.out.println("I don't understand that command.");
            }
            if(monstersHealth <= 0){
               loopControl = false;
               System.out.println("You defeated the goblin!");
            }
        }
    }

    public static void createMonster(){

        int monstersHealth = 100;
        int monsterAttackpower = 0;
        String monstersName;
        int xp;

        Random randomGenerator = new Random();

        int randomNumber = randomGenerator.nextInt(2);
        if (randomNumber == 0){
            monstersName = "Goblin";
            monstersHealth = 75 + randomGenerator.nextInt(24);
            monsterAttackpower = 8 + randomGenerator.nextInt(4);
            xp = 1;
        }
        else if (randomNumber == 1){
            monstersName = "Orc";
            monstersHealth = 100 + randomGenerator.nextInt(24);
            monsterAttackpower = 12 + randomGenerator.nextInt(4);
            xp = 3;
        }
        else if (randomNumber == 2){
            monstersName = "Troll";
            monstersHealth = 150 + randomGenerator.nextInt(49);
            monsterAttackpower = 15 + randomGenerator.nextInt(4);
            xp = 5;

                }
        }
}

最佳答案

如我所见,您的main方法中没有方法调用,尝试像这样在main方法中调用您的静态方法

public static void main(String[] args){

        int monstersHealth = 100;
        int monsterAttackpower = 0;
        String monstersName;
        int xp;
        int yourHealth = 0;
        int yourAttackpower = 0;
        int magicPower = 0;
        int magicPoints = 0;
        int statPoints = 20;
        createHero();
        createMonster();
        runCombat();
    }

关于java - 我的Java程序已编译但无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39586145/

相关文章:

Java string .length() X 不适合 DB2 varchar(X)

c - 错误: expected specifier-qualifier-list before "File"

c++ - 在 C++ 中,我收到一条消息 "error: ' void *' is not a pointer-to-object type"

c++ - 如何修复二进制运算符

asp.net - 构建时出现对象引用错误,但是…没有位置?

java - 编写 Java 教科书中的程序作为练习。我的内容和书上的一模一样,但它一直给我 "Cannot find symbol errors"

java - 位数组到字节 - java

java - 非常简单的自定义 taglib 函数不起作用

java - 问应届大学毕业生的 15 分钟 Java 好问题

java - 为什么我的递归 Java 方法中的字段会发生变化?