2014-02-19 24 views
1

我希望能有人指出我的方向。下面的代碼是我的雙人骰子游戲的程序。我需要添加第三名球員,但不知道如何。到目前爲止,我們已經討論了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"); 
     } 
    } 
} 
+2

你是否使用數組?因爲我要做的是將玩家數據存儲到一個bean中,然後創建一個'ArrayList ',它可以讓你存儲多個玩家(所以你可以擴展它成爲一個三人遊戲,四人遊戲等)。 –

+0

我們還沒有涉及到數組 - 但我確實知道一些學生已經將它用於這個特定的任務。那麼我將如何去使用數組?它看起來是該計劃的有效途徑?什麼是豆? (對不起,全新的) – user3058983

+0

沒關係!一個Java bean基本上是一個具有私有屬性的對象(它聽起來不像你已經涵蓋了面向對象編程)。這將是有效的,因爲你的Player bean將能夠保存'name','score'以及任何你想要爲每個用戶存儲的其他屬性。你可能想問問你的教授/老師是否可以使用數組。 –

回答

1

(假設你不能使用數組,對象等)

你會希望永遠不會有正常的設計像這樣。無論如何,幾乎所有以他們名字中的sP(第二玩家)和fP(第一玩家)爲變量的變量都需要第三個變量,可預測的tP(第三個玩家)。

然後,您可以開始複製粘貼並更改最終if-else語句中的邏輯以適合第三個玩家。

(好痛告訴你去做這樣的,只是承諾從來沒有設計類似這樣的事情在未來):)

+1

這是正確的,你是正確的;在不使用數組或對象的情況下考慮做這件事很痛苦 –

+0

哈哈,我們必須基於圖形設計來構建程序 - 所以別無選擇!我想它有很好的目的,因爲它有助於理解循環等。謝謝你的幫助:) – user3058983

+0

- 在我的if語句中,向最高的玩家添加回合,我對如何添加第三名玩家進入這個等式:if(dfPTot> dsPTot).........我是否需要做更多的陳述來覆蓋所有結果? – user3058983

1

你可以用通過它的Arraylist和循環,以保證每個玩家有它的機會。使用Arraylist的優點是您可以動態更改大小。所以你的實現應該適用於n個玩家。

這個片段允許你爲玩家初始化一個數組列表。你必須導入java.utils包。

ArrayList<String> players = new ArrayList<String>(); 

添加球員;先問問有多少玩家。通過輸入讀取,或使用常量。然後使用下一個添加n個玩家。 (max_players =球員數量)

for(int i = 1; i <= max_players; i++) { 
    System.out.println("\nPlayer " + i + ", please state your name: "); 
    String name =input.next(); 
    // add to array 
    players.Add(name); 
} 

一旦完成這個,你有一個球員名單。只需要爲遊戲本身再做一次。提示:使用兩個循環。一回合回合,一回合內線,每個玩家都有機會回合。

如果要訪問數組,你可以使用的方法,在this documentation

例如提供; players.size()給出陣列中當前玩家的數量。或players.get(0)給出陣列中的第一名球員的名字,並等...

請檢查文檔以獲得更多可能性。試着實現這樣的控制結構,最後你有一個動態代碼。我不打算提供一個完整的解決方案,首先嚐試一下。自我教育是理解這個概念的好方法。

如果您以後遇到實施問題,請隨時致電search it on stackoverflow或詢問是否找不到解決未來問題的方法。

祝你好運,快樂的編碼。 PS:實際上,使用對象(OOP)會使它更容易。只需Playernamescore字段。然後用它作爲數組的類型。

+2

(psst ...'「\ nPlayer」+ i' ...') –

+0

gosh,忽略它。謝謝 ! – KarelG

+0

我同意你的後記;這將是構想它的最佳方式。這一切都歸結爲數組和外部對象是否被允許或不允許(不幸的是)。 –