如何从方法中获取变量并在main中打印?

时间:2016-12-15 04:47:24

标签: java variables methods

我对java比较陌生,我必须在学校为一个项目构建一个简单的游戏,我试图从我的方法中得到一个变量," player1"并在主要打印。我已经尝试了好几个小时,但似乎无法让它发挥作用。

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提前谢谢! :)

1 个答案:

答案 0 :(得分:0)

一种方法是在类范围内声明变量(即:不在任何类方法中),并使它们成为静态 - 因为main方法是静态的,它只能访问类的静态成员,然后在静态方法中访问它们如下面的代码片段所示:

validates_presence_of