2016-12-15 3 views
0

私は比較的新しいJavaです。私は学校でプロジェクトのための単純なゲームを構築しなければなりません。私は自分のメソッド "player1"から変数を取得してmainに出力しようとしています。私はこれを何時間も試してきましたが、それを働かせるようには見えません。メソッドから変数を取り出してメインに出力するにはどうしたらいいですか?

CODE:誰かが私を助けることができればプロジェクトは数日中に起因しているよう

public class BoardGameProject 
{ public static void main (String[] args) 
{ 
Scanner keyboard = new Scanner (System.in); 

    System.out.println ("Welcome to My Board Game!"); 
    System.out.println ("How many players are there? (4 max)"); 
    int playerAmount = keyboard.nextInt(); 

    if (playerAmount == 1) 
{  
     player1(); 
} 
    else if (playerAmount == 2) 
{ 
     int p1rolls; // this is where I want to define the varibale "p1rolls" as the amount of rolls it takes to reach the end 
     int p2rolls; 

     player1(); 
     player2(); 

     if (p1rolls < p2rolls) 
     { 
      System.out.println ("Congratulations player 1, you won!"); 
      System.out.println ("You made it in " + p1rolls + " rolls"); 
      System.out.println ("Player 2 made it in " + p2rolls + " rolls"); 
     } 
     else if (p2rolls < p1rolls) 
     { 
      System.out.println ("Congratulations player 2, you won!"); 
      System.out.println ("You made it in " + p2rolls + " rolls"); 
      System.out.println ("Player 1 made it in " + p1rolls + " rolls"); 
     } 
} 
} 
    public static void player1() 
{ 
      Scanner keyboard = new Scanner (System.in);  

     int p1space = 0; 
     int p1health = 100; 
     int p1dollars = 0; 

     int rollValue = 0; 
     int p1rolls = 0; 

     System.out.println ("Player 1, Please Enter Your Name"); 
     keyboard.nextLine(); 
     String p1name = keyboard.nextLine(); 
     System.out.println ("Would you like a tutorial on how to play? (type yes or no)"); 
     String tutorial = keyboard.nextLine(); 
     if ((tutorial .equals ("yes"))) 
     { 
       System.out.println ("Okay, first the game will have you type roll to roll your die."); 
       System.out.println ("After that, depending on what number you got on your die, your character will move that far."); 
       System.out.println ("Certain spaces have traps and things to set you back so watch out! There are also spaces that give you bonuses such as extra health."); 
       System.out.println ("That should be enough for you to play. Have Fun! :)"); 
     } 
      while (p1health > 0) 
     { 
       System.out.println (p1name + " it's your turn!");  
       System.out.println ("Type roll to roll your die"); 
       String roll = keyboard.nextLine(); 
       if (roll .equals ("roll")) 
       { 
         int rollDie = (int) (6*Math.random()+1); 
         rollValue = rollDie; 
         p1space = p1space + rollValue; 
         p1rolls = p1rolls +1; 
         if (p1space == 0) 
          { 
           System.out.println ("START"); 
          } 
          else if (p1space == 1) 
          { 
           System.out.println ("SPACE 1"); 

          } 
          else if (p1space == 2) 
          { 
           System.out.println ("SPACE 2"); 
           System.out.println ("You found a med pack, +50 Health!"); 
           p1health = p1health +50; 
          } 
          else if (p1space == 3) 
          { 
           System.out.println ("SPACE 3"); 
           System.out.println ("You find a jetpack, move forward one space"); 
           p1space = p1space +1; 
          } 
          else if (p1space == 4) 
          { 
           System.out.println ("SPACE 4"); 
           System.out.println ("You are kidnapped, move back two spaces."); 
           p1space = p1space -2; 
          } 
          else if (p1space == 5) 
          { 
           System.out.println ("SPACE 5"); 

          } 
          else if (p1space == 6) 
          { 
           System.out.println ("SPACE 6"); 
           System.out.println ("You find $50!"); 
           p1dollars = p1dollars +25; 
          } 
          else if (p1space == 7) 
          { 
           System.out.println ("SPACE 7"); 
           System.out.println ("You find $100!"); 
           p1dollars = p1dollars +100; 
          } 
          else if (p1space == 8) 
          { 
           System.out.println ("SPACE 8"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p1health = p1health -25; 
          } 
          else if (p1space == 9) 
          { 
           System.out.println ("SPACE 9"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p1health = p1health -25; 
          } 
          else if (p1space == 10) 
          { 
           System.out.println ("SPACE 10"); 

          } 
          else if (p1space == 11) 
          { 
           System.out.println ("SPACE 11"); 
           System.out.println ("You found a med pack, +25 Health!"); 
           p1health = p1health +25; 
          } 
          else if (p1space == 12) 
          { 
           System.out.println ("SPACE 12"); 

          } 
          else if (p1space == 13) 
          { 
           System.out.println ("SPACE 13"); 
           System.out.println ("You find $50!"); 
           p1dollars = p1dollars +50; 
          } 
          else if (p1space == 14) 
          { 
           System.out.println ("SPACE 14"); 

          } 
          else if (p1space == 15) 
          { 
           System.out.println ("SPACE 15"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p1health = p1health -25; 
          } 
          else if (p1space == 16) 
          { 
           System.out.println ("SPACE 16"); 
           System.out.println ("You find $50!"); 
           p1dollars = p1dollars +50; 
          } 
          else if (p1space == 17) 
          { 
           System.out.println ("SPACE 17"); 

          } 
          else if (p1space == 18) 
          { 
           System.out.println ("SPACE 18"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p1health = p1health -25; 
          } 
          else if (p1space == 19) 
          { 
           System.out.println ("SPACE 19"); 
           System.out.println ("You are kidnapped, move back a space."); 
           p1space = p1space -1; 
          } 
          else if (p1space >= 20) 
          { 
           System.out.println ("--------------------------------------------------------------"); 
           System.out.println ("Congratulations, you made it to the end!"); 
           System.out.println ("You made it with:"); 
           System.out.println ("  " + p1health + " Health"); 
           System.out.println ("  " + p1dollars + " Dollars"); 
           System.out.println ("  " + p1rolls + " Rolls"); 
           p1health = 0; 
           Player1Rolls (p1rolls); // im trying to get "p1rolls" into my method "Player1Rolls" here so I can print this number in main 
          } 
         if (p1dollars >= 300) 
         { 
          System.out.println ("You have enough money to purchase a med pack. Would you like to buy one? (type yes or no)"); 
          String medPack = keyboard.nextLine(); 
          if (medPack .equals ("yes")) 
          { 
            p1health = p1health +50; 
            p1dollars = p1dollars -300; 
            System.out.println ("Health +100"); 
            System.out.println ("$300 removed"); 
          } 
         } 
       } 
       else 
       { 
          System.out.println ("I'm sorry I didn't quite get that. Type roll to roll your die."); 
       } 
     }//end of while 
} 
    public static void player2() 
{ 
    Scanner keyboard = new Scanner (System.in); 

    int p2health = 100; 
    int p2dollars = 0; 
    int p2space = 0; 

    int rollValue = 0; 
    int p2rolls = 0; 


     System.out.println ("Player 2, Please Enter Your Name"); 
     keyboard.nextLine(); 
     String p2name = keyboard.nextLine(); 
     System.out.println ("Would you like a tutorial on how to play? (type yes or no)"); 
     String tutorial = keyboard.nextLine(); 
     if ((tutorial .equals ("yes"))) 
     { 
       System.out.println ("Okay, first the game will have you type roll to roll your die."); 
       System.out.println ("After that, depending on what number you got on your die, your character will move that far."); 
       System.out.println ("Certain spaces have traps and things to set you back so watch out! There are also spaces that give you bonuses such as extra health."); 
       System.out.println ("That should be enough for you to play. Have Fun! :)"); 
     } 
      while (p2health > 0) 
     { 
       System.out.println (p2name + " it's your turn!");  
       System.out.println ("Type roll to roll your die"); 
       String roll = keyboard.nextLine(); 
       if (roll .equals ("roll")) 
       { 
         int rollDie = (int) (6*Math.random()+1);  
         rollValue = rollDie; 
         p2space = p2space + rollValue; 
         p2rolls = p2rolls +1; 
         if (p2space == 0) 
          { 
           System.out.println ("START"); 
          } 
          else if (p2space == 1) 
          { 
           System.out.println ("SPACE 1"); 

          } 
          else if (p2space == 2) 
          { 
           System.out.println ("SPACE 2"); 
           System.out.println ("You found a med pack, +50 Health!"); 
           p2health = p2health +50; 
          } 
          else if (p2space == 3) 
          { 
           System.out.println ("SPACE 3"); 
           System.out.println ("You find a jetpack, move forward one space"); 
           p2space = p2space +1; 
          } 
          else if (p2space == 4) 
          { 
           System.out.println ("SPACE 4"); 
           System.out.println ("You are kidnapped, move back two spaces."); 
           p2space = p2space -2; 
          } 
          else if (p2space == 5) 
          { 
           System.out.println ("SPACE 5"); 

          } 
          else if (p2space == 6) 
          { 
           System.out.println ("SPACE 6"); 
           System.out.println ("You find $50!"); 
           p2dollars = p2dollars +25; 
          } 
          else if (p2space == 7) 
          { 
           System.out.println ("SPACE 7"); 
           System.out.println ("You find $100!"); 
           p2dollars = p2dollars +100; 
          } 
          else if (p2space == 8) 
          { 
           System.out.println ("SPACE 8"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p2health = p2health -25; 
          } 
          else if (p2space == 9) 
          { 
           System.out.println ("SPACE 9"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p2health = p2health -25; 
          } 
          else if (p2space == 10) 
          { 
           System.out.println ("SPACE 10"); 

          } 
          else if (p2space == 11) 
          { 
           System.out.println ("SPACE 11"); 
           System.out.println ("You found a med pack, +25 Health!"); 
           p2health = p2health +25; 
          } 
          else if (p2space == 12) 
          { 
           System.out.println ("SPACE 12"); 

          } 
          else if (p2space == 13) 
          { 
           System.out.println ("SPACE 13"); 
           System.out.println ("You find $50!"); 
           p2dollars = p2dollars +50; 
          } 
          else if (p2space == 14) 
          { 
           System.out.println ("SPACE 14"); 

          } 
          else if (p2space == 15) 
          { 
           System.out.println ("SPACE 15"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p2health = p2health -25; 
          } 
          else if (p2space == 16) 
          { 
           System.out.println ("SPACE 16"); 
           System.out.println ("You find $50!"); 
           p2dollars = p2dollars +50; 
          } 
          else if (p2space == 17) 
          { 
           System.out.println ("SPACE 17"); 

          } 
          else if (p2space == 18) 
          { 
           System.out.println ("SPACE 18"); 
           System.out.println ("You are shot by an enemy, -25 Health."); 
           p2health = p2health -25; 
          } 
          else if (p2space == 19) 
          { 
           System.out.println ("SPACE 19"); 
           System.out.println ("You are kidnapped, move back a space."); 
           p2space = p2space -1; 
          } 
          else if (p2space >= 20) 
          { 
           System.out.println ("--------------------------------------------------------------"); 
           System.out.println ("Congratulations, you made it to the end!"); 
           System.out.println ("You made it with:"); 
           System.out.println ("  " + p2health + " Health"); 
           System.out.println ("  " + p2dollars + " Dollars"); 
           System.out.println ("  " + p2rolls + " Rolls"); 
           p2health = 0; 
           Player2Rolls(p2rolls);// im trying to get "p2rolls" into my method "Player2Rolls" here so I can print this number in main 
          } 
         if (p2dollars >= 300) 
         { 
          System.out.println ("You have enough money to purchase a med pack. Would you like to buy one? (type yes or no)"); 
          String medPack = keyboard.nextLine(); 
          if (medPack .equals ("yes")) 
          { 
            p2health = p2health +50; 
            p2dollars = p2dollars -300; 
            System.out.println ("Health +100"); 
            System.out.println ("$300 removed"); 
          } 
         } 
       } 
       else 
       { 
          System.out.println ("I'm sorry I didn't quite get that. Type roll to roll your die."); 
       } 
     }//end of while 
} 
    public static int Player1Rolls (int p1rolls) 
{ 
     return (p1rolls); // this is the method that i want to print in main} 
} 
public static int Player2Rolls (int p2rolls) 
{ 
     return (p2rolls); // and this one 
} 
} 

すると、それは素晴らしいことです。申し訳ありませんが、私のプログラムはとても長く、私はちょっとこのようなものを持つノブです:Pありがとうございます! :)

+0

コードをフォーマットできますか?また、グローバル変数を出力する変数を作成するか、その特定の変数を返すようにしてください。 –

+0

戻り値を返さない限り、メソッドから変数を取得することはできません。あなたは変数の範囲について読むべきです。特定のメソッドを実行した後に変数にアクセスする場合は、その変数を前もって定義する必要があります。 – Thrasher

+0

p1rollとp2rollは、使用する前に初期化されていません(行24,30)。 – leeyuiwah

答えて

0

1つの方法は、クラスメソッドの内部にない変数を宣言し、静的にすることです(メインメソッドは静的で、クラスの静的メンバーにのみアクセスでき、次のコードスニペットに示すように静的メソッドを使用します。

class BoardGameProject 
{ 
     //declare your variables in player mthods here as: 
     int p1space = 0; 
     int p1health = 100; 
     int p1dollars = 0; 

     int rollValue = 0; 
     int p1rolls = 0; 

     public static void main(String[] args) 
     { 
      System.out.println(p1rolls); 
      System.out.println(p1space); 
      System.out.println(p1dollars); 
      System.out.println(rollValue); 
      System.out.println(p1health); 

     } 
} 
+0

助けてくれてありがとう、私はそれを試してみよう! :) –

関連する問題