我的java程序编译但不会运行

时间:2016-09-20 05:13:32

标签: 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;

                }
        }
}

3 个答案:

答案 0 :(得分:1)

正如我所看到的,在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();
    }

答案 1 :(得分:0)

您没有调用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();
    runCombat();
}

答案 2 :(得分:0)

呀,你没有调用任何方法,并且不应该以这种方式使用java! 对于你的情况,没有使用在main方法中初始化变量你可以在main方法中调用函数(如果你在构造函数中调用函数而不是在main方法中调用函数而使用变量为类的实例变量,所以你可以在不同的方法中使用它们。)

public CombatCalculatorExpansion1{
    createHero();
    createMonster();
    runCombat();
}