Java 两人掷骰子的游戏,分数不超过50分

Java 两人掷骰子的游戏,分数不超过50分,java,arrays,random,Java,Arrays,Random,我正在创建一个游戏,其中两名玩家相互掷骰子 两个人(玩家A、玩家B)玩骰子游戏。他们每轮掷一个骰子,数字越高,获胜者得5分。如果两个人掷的骰子值相同,他们都得3分。如果一名玩家达到50分或更高,游戏结束。如果两名玩家在同一轮都达到50分,那么这将成为平局,并且需要再进行一轮比赛,直到更高价值的玩家赢得比赛(如果第11轮仍然平局,则进行下一轮比赛。这是我目前的代码,但我没有得到任何分数达到50分才能结束比赛 public static void main(String[] args) {

我正在创建一个游戏,其中两名玩家相互掷骰子

两个人(玩家A、玩家B)玩骰子游戏。他们每轮掷一个骰子,数字越高,获胜者得5分。如果两个人掷的骰子值相同,他们都得3分。如果一名玩家达到50分或更高,游戏结束。如果两名玩家在同一轮都达到50分,那么这将成为平局,并且需要再进行一轮比赛,直到更高价值的玩家赢得比赛(如果第11轮仍然平局,则进行下一轮比赛。这是我目前的代码,但我没有得到任何分数达到50分才能结束比赛

public static void main(String[] args) {
    
    int playerA[] = new int[10];
    int playerB[] = new int[10];
    int playerAScore = 0;
    int playerBScore = 0;
    int round = 1;
    
    for (int i =0; i < playerA.length; i++) {
        System.out.println("Roll the die for round " + round++);
        playerA[i] = (int) ((Math.random() * 6) + 1);
        playerB[i] = (int) ((Math.random() * 6) + 1);
    
        System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);
    
        if(playerA[i] == playerB[i]) {
            playerAScore = playerAScore + 3;
            playerBScore = playerBScore + 3;
        }
        else if (playerA[i] > playerB[i]) {
            playerAScore = playerAScore + 5;
        }
        else if (playerB[i] > playerB[i]) {
            playerBScore = playerBScore + 5;
        }
        if(playerAScore >= 50 || playerBScore >= 50) {
            break;
        }
    }
    
    System.out.println("The game is over.");
    
    if(playerAScore >= playerBScore)
        System.out.println("The winner is player A");
    else
        System.out.println("The winner is player B");
    
    System.out.println("How many rounds of game played?");
    System.out.println("Rounds played: " + round);
    
    System.out.println("Total Score per player:");
    System.out.println("Player A score: " + playerAScore);
    System.out.println("Player B score: " + playerBScore);
    
}
publicstaticvoidmain(字符串[]args){
int playerA[]=新int[10];
int playerB[]=新int[10];
int playerAScore=0;
int playerBScore=0;
整数轮=1;
for(int i=0;iplayerB[i]){
playerAScore=playerAScore+5;
}
else if(playerB[i]>playerB[i]){
playerBScore=playerBScore+5;
}
如果(playerScore>=50 | | playerScore>=50){
打破
}
}
System.out.println(“游戏结束了”);
如果(playerScore>=playerScore)
System.out.println(“获胜者是玩家A”);
其他的
System.out.println(“获胜者是玩家B”);
System.out.println(“玩了多少回合的游戏?”);
System.out.println(“播放的回合数:+回合数”);
System.out.println(“每个玩家的总分:”);
System.out.println(“玩家A分数:+playerAScore”);
System.out.println(“玩家B分数:+playerScore”);
}
}

  • 在分数比较中,你有一个输入错误
  • 这总是错误的,所以一个球员根本得不到他的分数

            else if (playerB[i] > playerB[i]) {
                playerBScore = playerBScore + 5;
            }
    
    片段。显然应该是:

            else if (playerB[i] > playerA[i]) {
                playerBScore = playerBScore + 5;
            }
    
  • 第二个错误可能是你只玩了
    playerA.length
    次。在这种情况下是10次,所以玩家只有在赢得所有游戏的情况下才能得到50次(概率非常低)

  • 末尾显示的轮数错误。播放的轮数为
    10
    ,但它再次递增,并表示轮数为
    11

  • 在分数比较中,你有一个输入错误
  • 这总是错误的,所以一个球员根本得不到他的分数

            else if (playerB[i] > playerB[i]) {
                playerBScore = playerBScore + 5;
            }
    
    片段。显然应该是:

            else if (playerB[i] > playerA[i]) {
                playerBScore = playerBScore + 5;
            }
    
  • 第二个错误可能是你只玩了
    playerA.length
    次。在这种情况下是10次,所以玩家只有在赢得所有游戏的情况下才能得到50次(概率非常低)

  • 末尾显示的轮数错误。播放的轮数为
    10
    ,但它再次递增,并表示轮数为
    11


  • 你应该使用
    while
    ,而不是
    for
    循环。在你的情况下,每次必须有一名玩家获胜才能获得50分。试试这个:
    while(playerAScore<50&&playerBScore<50)
    和循环内的增量索引
    i

    你应该使用
    while
    ,而不是
    循环。在你的情况下,每次必须有一名玩家获胜才能得到50分。试试这个:
    while(playerAScore<50&&playerscore<50)
    和循环内的增量索引
    i

    其他人指出了
    PlayerB>PlayerB
    的缺陷,但是这个答案是针对如何解决只有玩家10轮的问题。与其限制为10轮,不如允许函数递归执行,直到达到50分

    publicstaticvoidmain(字符串[]args){
    游戏=新游戏();
    游戏。回合(50);
    System.out.println(“游戏结束了”);
    如果(game.playerScore>=game.playerScore)
    System.out.println(“获胜者是玩家A”);
    其他的
    System.out.println(“获胜者是玩家B”);
    System.out.println(“玩了多少回合的游戏?”);
    System.out.println(“已玩回合:+游戏回合”);
    System.out.println(“每个玩家的总分:”);
    System.out.println(“玩家A分数:+game.playerAScore”);
    System.out.println(“玩家B分数:+game.playerScore”);
    }
    
    静态类游戏{
    私有整数playerAScore=0;
    私人int PlayerScore=0;
    私人整数回合;
    无效回合(整数最大分数){
    System.out.println(“滚圆模具”+滚圆);
    int playerRoll=(int)((Math.random()*6)+1);
    int playerBRoll=(int)((Math.random()*6)+1);
    System.out.println(“玩家A有“+playerRoll+”,玩家B有“+playerRoll+”);
    如果(playerRoll==playerRoll){
    playerAScore+=3;
    球员得分+=3分;
    }否则如果(播放滚动>播放滚动){
    playerAScore+=5;
    }否则{
    球员得分+=5分;
    }
    如果(PlayerScore>=maximumScore | | PlayerScore>=maximumScore){
    回来
    }
    round++;
    四舍五入(最高分数);
    }
    }
    
    输出

    Roll the die for round 0
    player A has 2 and player B has 2
    Roll the die for round 1
    player A has 2 and player B has 2
    Roll the die for round 2
    player A has 6 and player B has 6
    Roll the die for round 3
    player A has 6 and player B has 6
    Roll the die for round 4
    player A has 6 and player B has 3
    Roll the die for round 5
    player A has 5 and player B has 2
    Roll the die for round 6
    player A has 2 and player B has 2
    Roll the die for round 7
    player A has 3 and player B has 1
    Roll the die for round 8
    player A has 2 and player B has 4
    Roll the die for round 9
    player A has 6 and player B has 6
    Roll the die for round 10
    player A has 1 and player B has 4
    Roll the die for round 11
    player A has 2 and player B has 3
    Roll the die for round 12
    player A has 2 and player B has 5
    Roll the die for round 13
    player A has 6 and player B has 4
    Roll the die for round 14
    player A has 1 and player B has 2
    Roll the die for round 15
    player A has 1 and player B has 5
    Roll the die for round 16
    player A has 3 and player B has 4
    The game is over.
    The winner is player B
    How many rounds of game played?
    Rounds played: 16
    Total Score per player:
    Player A score: 38
    Player B score: 53
    

    其他人已经指出了
    PlayerB>PlayerB
    的缺陷,但是这个答案是针对如何解决只有玩家10轮的问题。与其限制为10轮,不如允许函数递归执行,直到
    public class diceGame{
    public static void main(String[] args) {
        int playerA[] = new int[10];
        int playerB[] = new int[10];
        int playerAScore = 0;
        int playerBScore = 0;
        int round = 1;
        
        for (int i =0; i < playerA.length; i++) {
            System.out.println("Roll the die for round " + round++);
            playerA[i] = (int) ((Math.random() * 6) + 1);
            playerB[i] = (int) ((Math.random() * 6) + 1);
        
            System.out.println("player A has " + playerA[i] + " and player B has " + playerB[i]);
        
            if(playerA[i] == playerB[i]) {
                playerAScore = playerAScore + 3;
                playerBScore = playerBScore + 3;
            }
            else if (playerA[i] > playerB[i]) {
                playerAScore = playerAScore + 5;
            }
            else if (playerB[i] > playerA[i]) {
                playerBScore = playerBScore + 5;
            }
            if(playerAScore >= 50 || playerBScore >= 50) {
                break;
            }
        }
        
        System.out.println("The game is over.");
        
        if(playerAScore >= playerBScore)
            System.out.println("The winner is player A");
        else
            System.out.println("The winner is player B");
        
        System.out.println("How many rounds of game played?");
        System.out.println("Rounds played: " + round);
        
        System.out.println("Total Score per player:");
        System.out.println("Player A score: " + playerAScore);
        System.out.println("Player B score: " + playerBScore);
        
    }
    }