我希望能有人指出我的方向。下面的代碼是我的雙人骰子游戲的程序。我需要添加第三名球員,但不知道如何。到目前爲止,我們已經討論了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'以及任何你想要爲每個用戶存儲的其他屬性。你可能想問問你的教授/老師是否可以使用數組。 –