2014-02-19 22 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

我同意你的后记;这将是构想它的最佳方式。这一切都归结为数组和外部对象是否被允许或不允许(不幸的是)。 –