我希望能有人指出我的方向。下面的代码是我的双人骰子游戏的程序。我需要添加第三名球员,但不知道如何。到目前为止,我们已经讨论了else语句,开关和循环 - 所以我不允许使用其他任何东西,因为我们还没有涉及它。我已经查看了这些问题,但是我没有找到任何答案来回答我的问题,请问有人可以帮忙吗?如何添加第三个玩家到我的简单java骰子游戏?
import java.util.Scanner;
class dice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String fP, sP;
int dice = 0, dfPTot = 0, dsPTot = 0, round = 0, fPScore = 0, sPScore = 0, fPScoreR = 0,
sPScoreR = 0;
System.out.println(
"Welcome to the online interactive dice game.\n\n\t
* To complete the 5 rounds you will need two players and three dice
!");
System.out.println("\nPlayer 1, please state your name: ");
fP = input.next();
System.out.println("Welcome " + fP + "\n\nPlayer 2, please state your name: ");
sP = input.next();
System.out.println("Welcome " + sP + "\n\nLet's begin!");
for (int count = 1; count <= 5; count++) {
System.out.print(fP + " please throw your three dices and then input
your total dice score
: ");
dfPTot = input.nextInt();
System.out.print(sP + " please throw your three dices and then input
your total dice score
: ");
dsPTot = input.nextInt();
round = dfPTot + dsPTot;
System.out.print(" The round total is: " + round + " \n");
if (dfPTot > dsPTot) {
fPScore = fPScore + round;
fPScoreR += fPScore;
sPScore = sPScore + 0;
sPScoreR += sPScore;
} else {
sPScore = sPScore + round;
sPScoreR += sPScore;
fPScore = fPScore + 0;
fPScoreR += fPScore;
}
}
dfPTot = fPScore = round = 0;
fPScore = fPScore + fPScoreR;
sPScore = sPScore + sPScoreR;
if (fPScore > sPScore) {
System.out.println(fP + " is the Dice Master scoring: " + fPScore + " points");
} else {
System.out.println(sP + " is the Dice Master scoring: " + sPScore + " points");
}
}
}
你是否使用数组?因为我要做的是将玩家数据存储到一个bean中,然后创建一个'ArrayList',它可以让你存储多个玩家(所以你可以扩展它成为一个三人游戏,四人游戏等)。 –
我们还没有涉及到数组 - 但我确实知道一些学生已经将它用于这个特定的任务。那么我将如何去使用数组?它看起来是该计划的有效途径?什么是豆? (对不起,全新的) – user3058983
没关系!一个Java bean基本上是一个具有私有属性的对象(它听起来不像你已经涵盖了面向对象编程)。这将是有效的,因为你的Player bean将能够保存'name','score'以及任何你想要为每个用户存储的其他属性。你可能想问问你的教授/老师是否可以使用数组。 –