如何在 Java 中初始化变量/增加“轮数” - 初学者,需要帮助

我正在尝试创建一个在 Java 中模拟骰子游戏的程序。有两名玩家每轮掷 3 个骰子,共 6 轮。不同的数字给每个玩家不同的分数,这些分数在每一轮中累积。


我需要帮助弄清楚如何增加回合数 - 老实说,我认为我的整个事情都是一团糟,但我希望这能让我走上正轨。


我尝试过的内容显示在我的代码中。基本上,我可以为三个骰子生成掷骰并计算掷骰的分数,但是当我必须隔离到一个单轮然后添加这些轮时,我会卡住。


您可以在我的评论中看到我的问题所在:getScore() 方法和 playBunco() 方法。


[CODE REMOVED FOR BREVITY] 


public static int diceRoll() {

  int roll;

  roll = (int)(Math.random() * 6 + 1); 

  return roll;

}


public static int getScore() {

  diceRoll(); 

  int roundNumber; 

我如何在 main 方法中编写这个(int roundNumber),以便我可以在这里将它用作 1 到 6 之间的数字并在其他方法中逐轮增加它?


[CODE REMOVED FOR BREVITY] 

  int score = 0; 

    if(die1 == roundNumber) { 

      if(die2 == roundNumber) {

        if(die3 == roundNumber) {

          score = bunco;

        }

        else{

          score = twoPoints;

        }

      }

      else{

        score = onePoint;

      }

  [CODE REMOVED FOR BREVITY] 

  return score;

}


public static void playOneRound() {


  diceRoll(); 

  int die1 = diceRoll(); 

  int die2 = diceRoll(); 

  int die3 = diceRoll(); 


  getScore(); 

  int score = getScore(); 


  String player; 


  for(int roundNumber = 1; roundNumber <= 3; roundNumber ++) {

    System.out.println(player + " rolled " + die1 + die2 + 

    die3 + " and scored " + score + " points");

  }

}


public static void playBunco() {

  String player1; 

  String player2; 

在这里,我需要显示来自 playOneRound() 的打印语句,但我需要每轮更改它并增加分数问题:1. 显示三个卷 (abc),2. 增加轮数,3. 每轮加分.


}


public static void main(String[] args) {

  diceRoll(); 

  getScore();

  String player1 = (args[0]); 

  String player2 = (args[1]); 

  playBunco(); //this is where I just got completely stuck

}

}


我的代码没有崩溃(主要是因为我无法编写任何代码来解决我的问题),但它确实说 int roundNumber 没有初始化(这是我的另一个问题)。


我是 Java 的新手,我知道这可能是一个相当愚蠢的问题 - 感谢您抽出宝贵的时间!


烙印99
浏览 86回答 1
1回答

MMMHUHU

你的代码是一团糟。我已将其重新编码为接近您的代码并添加了解释。我希望你能理解并从我的代码中学习。随时发表评论以进行澄清。此外,您可能想学习 Java 基础知识。/**&nbsp;*&nbsp;*&nbsp;*/public class Main {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The lines below are deleted since they are not needed. Your game is just&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* about to start. No need to roll the dice and get the score&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; // diceRoll();&nbsp; &nbsp; &nbsp; &nbsp; // getScore();&nbsp; &nbsp; &nbsp; &nbsp; String player1 = args[0]; // Deleted the unnecessary parenthesis. Previously was (args[0])&nbsp; &nbsp; &nbsp; &nbsp; String player2 = args[1]; // Deleted the unnecessary parenthesis. Previously was (args[1])&nbsp; &nbsp; &nbsp; &nbsp; playBunco(player1, player2); // Pass the player1 and player2 variables to playBunco, so that playBunco method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // will have access to the players&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This will be the method that the "play" part will be processed&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param player1 The name of player 1&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param player2 The name of player 2&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void playBunco(String player1, String player2) {&nbsp; &nbsp; &nbsp; &nbsp; int player1AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // since&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you have 6 rounds&nbsp; &nbsp; &nbsp; &nbsp; int player2AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // since&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you have 6 rounds&nbsp; &nbsp; &nbsp; &nbsp; int totalNumberOfRounds = 6; // this is the total number of rounds&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* This loop is needed to play all the rounds. In this case until round 6 int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* currentRoundNumer = 1 -> it means that the round should start at round 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* currentRoundNumer < totalNumberOfRounds -> this means that the process inside&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the loop will be repeatedly executed up until the total number of rounds, in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* this case round 6 currentRoundNumer++ -> this means that we should increment&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the currentRoundNumber after every round&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; for (int currentRoundNumber = 1; currentRoundNumber <= totalNumberOfRounds; currentRoundNumber++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // I am assuming that player1 will take his/her turn first and then followed by&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // player2's turn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("====== Start of round " + currentRoundNumber + " ====== ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int player1RoundScore = playOneRound(currentRoundNumber, player1); // This will generate player1's turn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player1AllScore[currentRoundNumber - 1] = player1RoundScore; // This will save the score of player 1 in this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // round. As you can see I decremented 1 to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the current round number, it is because&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the array index starts at 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int player2RoundScore = playOneRound(currentRoundNumber, player2); // This will generate player2's turn&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; player2AllScore[currentRoundNumber - 1] = player2RoundScore; // This will save the score of player 2 in this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // round. As you can see I decremented 1 to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the current round number, it is because&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the array index starts at 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("====== End of round " + currentRoundNumber + " ====== \n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* You can do some calculations here regarding the scores of each player. To get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the score you can do this: player1AllScore[0]; The above code will get the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* round 1 score of player 1. Do not forget that you should decrement 1 in each&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* round. so if you want to get the score of player 1 on round 2, you will pass&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* '1', thus it should be player1AllScore[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; }&nbsp; &nbsp; public static int diceRoll() {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The lines that are deleted below is correct, but since you do not use roll&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* variable in any other process, you can just directly return the generated&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* random, number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; // int roll;&nbsp; &nbsp; &nbsp; &nbsp; // roll = (int) (Math.random() * 6 + 1);&nbsp; &nbsp; &nbsp; &nbsp; // return roll;&nbsp; &nbsp; &nbsp; &nbsp; return (int) (Math.random() * 6 + 1);&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This will be the method that will calculate the scores of a player Since you&nbsp; &nbsp; &nbsp;* are using roundNumber from your previous code, I am assuming that you are&nbsp; &nbsp; &nbsp;* calculating the score in each round&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param roundNumber The current round&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param dice1 The value of dice 1&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param dice2 The value of dice 2&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param dice3 The value of dice 3&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static int getScore(int roundNumber, int dice1, int dice2, int dice3) {&nbsp; &nbsp; &nbsp; &nbsp; // diceRoll(); <- you do not need this since you are not rolling the dice, you&nbsp; &nbsp; &nbsp; &nbsp; // are just about to calculate the score&nbsp; &nbsp; &nbsp; &nbsp; // int roundNumber; <- you do not need this since it is already passed as a&nbsp; &nbsp; &nbsp; &nbsp; // parameter&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Starting from this point I do not actually understand what you are trying to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* do so I have just based in on assumptions.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; // [CODE REMOVED FOR BREVITY]&nbsp; &nbsp; &nbsp; &nbsp; // int score = 0;&nbsp; &nbsp; &nbsp; &nbsp; // if(die1 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; // if(die2 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; // if(die3 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; // score = bunco;&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; // else{&nbsp; &nbsp; &nbsp; &nbsp; // score = twoPoints;&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; // else{&nbsp; &nbsp; &nbsp; &nbsp; // score = onePoint;&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; // [ CODE REMOVED FOR BREVITY]&nbsp; &nbsp; &nbsp; &nbsp; // return score;&nbsp; &nbsp; &nbsp; &nbsp; int score = 0; // initialize the score&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* just my assumption since from the code above, I think when all the dice&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* values are equal to the round number you will have a bonus points? If so then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* the above condition will satisfy it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; if (dice1 == roundNumber && dice2 == roundNumber && dice3 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = 1000; // you do not need to use a variable "bunco", you can directly assign a value.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* just my assumption since from the code above, I think when only dice1 and&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* dice2 values are equal to the round number you will have a two points? If so&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* then the above condition will satisfy it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; else if (dice1 == roundNumber && dice2 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* just my assumption since from the code above, I think when only dice1 values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* are equal to the round number you will have a one point? If so then the above&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* condition will satisfy it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; else if (dice1 == roundNumber) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return score;&nbsp; &nbsp; }&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;* This will be the method that will do 1 round. It will roll the 3 dice and&nbsp; &nbsp; &nbsp;* calculate the score&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param roundNumber The current round&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param player The name of the player who is playing this round&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static int playOneRound(int roundNumber, String player) {&nbsp; &nbsp; &nbsp; &nbsp; // diceRoll(); <- you do not need this&nbsp; &nbsp; &nbsp; &nbsp; int die1 = diceRoll();&nbsp; &nbsp; &nbsp; &nbsp; int die2 = diceRoll();&nbsp; &nbsp; &nbsp; &nbsp; int die3 = diceRoll();&nbsp; &nbsp; &nbsp; &nbsp; // getScore(); <- you do not need this&nbsp; &nbsp; &nbsp; &nbsp; int score = getScore(roundNumber, die1, die2, die3);&nbsp; &nbsp; &nbsp; &nbsp; // String player; <- you do not need this&nbsp; &nbsp; &nbsp; &nbsp; // Since this methods goal is to play 1 round you do not have to loop this&nbsp; &nbsp; &nbsp; &nbsp; // for (int roundNumber = 1; roundNumber <= 3; roundNumber++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(player + " rolled " + die1 + "," + die2 + "," + die3 + " and scored " + score + " points");&nbsp; &nbsp; &nbsp; &nbsp; // }&nbsp; &nbsp; &nbsp; &nbsp; // return the score for calculating the total score for all the rounds&nbsp; &nbsp; &nbsp; &nbsp; return score;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java