简体   繁体   English

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

[英]How do I get a variable out of a method and print it in main?

I am relatively new to java and I have to build a simple game for a project in school and I am trying to get a variable out of my method, "player1" and print it in main. 我是Java的新手,我必须为学校的一个项目构建一个简单的游戏,我试图从我的方法“ player1”中获取一个变量并将其打印在main中。 I've been trying this for hours and I can't seem to get it to work. 我已经尝试了几个小时,但似乎无法正常工作。

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
 }
}

If someone could help me, that would be great as the project is due in a couple of days. 如果有人可以帮助我,那将很棒,因为该项目将在几天之内完成。 Sorry my program is so long and that I'm kinda a noob with this stuff :P Thanks in advance! 抱歉,我的程序太长了,我对这个东西有点不了解:P预先感谢! :) :)

One way is to declare variables in class scope(ie:not inside any class methods), and make them static-as main method is static and it can access only static members of class,and then access them in static method as shown below in following code snippet: 一种方法是在类范围内声明变量(即:不在任何类方法内),并使它们成为静态对象,因为main方法是静态的,它只能访问该类的静态成员,然后按如下所示在static方法中对其进行访问以下代码段:

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);

      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM