我对 java 比较陌生,对于一个项目,我正在制作一个最多 4 名玩家的游戏,但一开始它会询问有多少玩家,所以我不需要 main 中的所有 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!");
}
}
最佳答案
将玩家作为一个类。类具有玩家姓名等属性和 startGame 等方法。您还可以向用户提供玩家选择的数量,并根据需要创建玩家对象。
关于java - 如何调用 main 中的方法并告诉它运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41153378/