2016-09-20 92 views
-1

我的java程序编译但是当我尝试运行它时不会返回任何东西。我正在使用多种方法进行分配,并且我不确定是否对可能对程序没有任何影响的方法做了错误处理。我的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; 

       } 
     } 
} 
+0

JavaScript不是Java。 – Li357

+0

java!== javascript –

+2

在其主函数中不会调用任何方法 –

回答

1

,我可以看到有你的主要方法没有方法调用尝试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(); 
    } 
0

你是不是在主函数中调用任何方法。因此,它只是创建您在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(); 
} 
0

雅你没有调用任何方法来创建流和Java不应该被这种方式使用! 你的情况没有使用初始化主要方法中的变量,你可以调用main方法中的函数(根据我的打浆机,如果你在构造函数中调用函数而不是调用main方法中的函数并使用变量作为类的实例变量,所以你可以使用不同方法中的那些)。

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