java骰子游戏问题 - 快请

时间:2016-12-20 10:41:04

标签: java methods

创建一个骰子游戏,它是玩家与电脑。游戏的目的是轮流掷骰子,第一个达到100胜。如果任一玩家掷出2,则他们的总数自动下降2.如果他们发挥作用,则将其添加到运行总计中。拳头1> = 100胜。生成0到2之间的随机数,以确定谁先滚动。如果为0,则播放器首先滚动,如果一个,则计算机首先滚动。

我是高中新手程序员,我遇到很多麻烦。我想出了如何通过单独列出它们来单独显示骰子卷。如果有人能给我一个更简单的方法会很好,因为我无法弄清楚如何使用该方法找到第一个到100个。

public class problemTwo {

    public static void main(String[] args) {

        // TODO Auto-generated method stub
        int d1=(int)(Math.random()*6+1);
        int d2=(int)(Math.random()*6+1);
        int dSum= d1 + d2;
        System.out.println("Player Roll 1 total is ;" +dSum); 

        int d3=(int)(Math.random()*6+1);
        int d4=(int)(Math.random()*6+1);
        int dSum2= d3 + d4;
        System.out.println("Player Roll 2 total is ;" +dSum2); 

        int d5=(int)(Math.random()*6+1);
        int d6=(int)(Math.random()*6+1);
        int dSum3= d5 + d6;
        System.out.println("Player Roll 3 total is ;" +dSum3); 

        int d7=(int)(Math.random()*6+1);
        int d8=(int)(Math.random()*6+1);
        int dSum4= d7 + d8;
        System.out.println("Player Roll 4 total is ;" +dSum4); 

        int d9=(int)(Math.random()*6+1);
        int d10=(int)(Math.random()*6+1);
        int dSum5= d9 + d10;
        System.out.println("Player Roll 5 total is ;" +dSum5); 

        int d11=(int)(Math.random()*6+1);
        int d12=(int)(Math.random()*6+1);
        int dSum6= d11 + d12;
        System.out.println("Player Roll 6 total is ;" +dSum6); 

        int d13=(int)(Math.random()*6+1);
        int d14=(int)(Math.random()*6+1);
        int dSum7= d13 + d14;
        System.out.println("Player Roll 7 total is ;" +dSum7); 

        int d15=(int)(Math.random()*6+1);
        int d16=(int)(Math.random()*6+1);
        int dSum8= d15 + d15;
        System.out.println("Player Roll 8 total is ;" +dSum8); 

        int d17=(int)(Math.random()*6+1);
        int d18=(int)(Math.random()*6+1);
        int dSum9= d17 + d17;
        System.out.println("Player Roll 9 total is ;" +dSum9); 

        int d19=(int)(Math.random()*6+1);
        int d20=(int)(Math.random()*6+1);
        int dSum10= d19 + d20;
        System.out.println("Player Roll 10 total is ;" +dSum10); 

     System.out.println("Roll "+ " total is"  +(dSum + dSum2 + dSum3 + dSum4 + dSum5+ + dSum6+ + dSum7+ + dSum8+ + dSum9+ + dSum10)); 
    }

}

1 个答案:

答案 0 :(得分:1)

这里的问题是,你是在限制掷骰子的时间。在你的代码中,每个玩家最多滚动时间10次,这意味着如果他们在每次滚动时得分低,他们将永远不会达到100。 你想要做的是使用一个while循环来运行相同的代码,直到达到条件。 这是骨架,我会让你弄明白其余部分;)

while( no player reached a score of 100 ) {
    player 1 roll the dice
    add the result to player 1 score
    player 2 roll the dice
    add the result to player 2 score
} 

请注意,此类网站上的此类问题并未获得好评。您需要做的是了解您可以开始工作的代码的哪一部分。发布作业只会让你得到负面投票(特别是在#34;快请#34;标题中)并最终删除你的问题。尝试将标题更改为Simulate dice game between two player之类的内容并重写问题。