如何在main中调用一个方法并告诉它运行?

时间:2016-12-14 22:32:41

标签: java methods

我相对较新的java和一个项目我正在制作一个最多有4个玩家的游戏,但最初它会询问有多少玩家,所以我不需要主游戏中的所有4个玩家的脚本。我试图让每个玩家都成为一种方法,并且根据玩家的数量,这将是有多少玩家的剧本。

基本上我不知道如何使用if语句中的一个命令来运行玩家的方法。

如果声明:

   if (playerAmount == 1)
{    
       run (player1); // this was my attempt to run the methods xD
}
    else if (playerAmount == 2)
{
       run (player1);
       run (player2); // this was my attempt to run the methods xD
}
    else if (playerAmount == 3)
{
      run (player1);
      run (player2);
      run (player3); // this was my attempt to run the methods xD
}
    else if (playerAmount == 4)
{
      run (player1);
      run (player2); 
      run (player3);
      run (player4); // this was my attempt to run the methods xD
}

方法(每个玩家都有一个,这只适用于玩家1):

    public static void player1()
  {
        Scanner keyboard = new Scanner (System.in);    

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

        int rollValue = 0;
        int rollDie = (int) (6*Math.random()+1);


        System.out.println ("Player 1, Please Enter Your Name");
        String p1name = keyboard.nextLine();
        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"))
                {
                         rollValue = rollDie;
                         p1space = p1space + rollValue;
                         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");
                                p1health = 0;
                            }
                         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;
                                    System.out.println ("Health +100");
                                    System.out.println ("$750 removed");
                             }
                         }
                }
                else 
                {
                            System.out.println ("I'm sorry I didn't quite get that. Type roll to roll your die.");
                }
        }//end of while
        if (p1health <= 0) // edit in multiplayer
        {
                    System.out.println ("Game Over");
                    System.out.println ("You Died!");
        }

}

3 个答案:

答案 0 :(得分:0)

让玩家成为一个班级。并且类具有诸如播放器名称和诸如startGame之类的方法之类的属性。此外,您还可以向玩家提供玩家选择的数量,并根据需要创建玩家对象。

答案 1 :(得分:0)

我不确定这是否是您需要的,但您可以考虑使用可运行的界面

private Runnable myRunnable = new Runnable() {
        public void run() {
           // do your stuff here

        }
    };

然后你可以像这样运行它

myRunnable.run();

答案 2 :(得分:0)

直接回答你的问题。由于您的方法名称为player1。要调用方法,请编写方法名称后跟一对括号:

myMethod();

所以你的代码应该是这样的:

if (playerAmount == 1)
{    
    player1();  //run method for player1
}

您可能已经意识到所有方法中都有大量的代码重复(player1(),player2(),player3()和player4())。您可能想学习如何将参数传递给方法。这样,您只需要实现一次:

public static void player(int playerNo){

}

知道如何使用方法之后。您可能还想看看Java类,您可以将每个播放器实际实现为对象而不是方法:

class Player{
    public void runScript(){

    }
}