2016-09-20 10 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

はmain関数には何も呼び出されません –

答えて

1

私はあなたのメインメソッドにはメソッドの呼び出しはあなたが主な機能で方法のいずれかを呼び出していません。この

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メソッド内の静的メソッドを呼び出すしようがありません見ることができるように。したがって、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(); 
} 
関連する問題