如何初始化变量/have increasing“;轮数;在Java中-初学者,需要帮助吗
我正在尝试创建一个用Java模拟骰子游戏的程序。有两名玩家每轮掷3个骰子,共6轮。不同的数字给每个玩家不同的分数,每轮都会累积 我需要帮助找出如何增加轮次-老实说,我认为我的整个事情是一团糟,但我希望这会让我走上正确的轨道 我的代码中显示了我的尝试。基本上,我可以为三个骰子生成掷骰,并计算掷骰的分数,但当我必须孤立到一个回合,然后添加这些回合时,我会陷入困境 您可以看到我在注释中遇到的问题,可以在方法getScore()和方法playBunco()中找到 如何在main方法中写入这个(int roundNumber),以便在这里将其用作1到6之间的数字,并在其他方法中逐轮增加它如何初始化变量/have increasing“;轮数;在Java中-初学者,需要帮助吗,java,loops,command-line,conditional,Java,Loops,Command Line,Conditional,我正在尝试创建一个用Java模拟骰子游戏的程序。有两名玩家每轮掷3个骰子,共6轮。不同的数字给每个玩家不同的分数,每轮都会累积 我需要帮助找出如何增加轮次-老实说,我认为我的整个事情是一团糟,但我希望这会让我走上正确的轨道 我的代码中显示了我的尝试。基本上,我可以为三个骰子生成掷骰,并计算掷骰的分数,但当我必须孤立到一个回合,然后添加这些回合时,我会陷入困境 您可以看到我在注释中遇到的问题,可以在方法getScore()和方法playBunco()中找到 如何在main方法中写入这个(int r
[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;
}
我的代码没有崩溃(主要是因为我无法编写任何代码来解决我的问题),但它确实说明int roundNumber没有初始化(这是我的另一个问题)
我对Java非常陌生,我知道这可能是一个相当愚蠢的问题——感谢您抽出时间 你的代码乱七八糟。我已经重新编码,尽可能接近您的代码,并添加了解释。我希望你能理解和学习我的代码。请随意评论以获得澄清。此外,您可能还想学习Java基础知识
/**
*
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
* The lines below are deleted since they are not needed. Your game is just
* about to start. No need to roll the dice and get the score
*/
// diceRoll();
// getScore();
String player1 = args[0]; // Deleted the unnecessary parenthesis. Previously was (args[0])
String player2 = args[1]; // Deleted the unnecessary parenthesis. Previously was (args[1])
playBunco(player1, player2); // Pass the player1 and player2 variables to playBunco, so that playBunco method
// will have access to the players
}
/*
* This will be the method that the "play" part will be processed
*
* @param player1 The name of player 1
*
* @param player2 The name of player 2
*/
public static void playBunco(String player1, String player2) {
int player1AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's
// since
// you have 6 rounds
int player2AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's
// since
// you have 6 rounds
int totalNumberOfRounds = 6; // this is the total number of rounds
/*
* This loop is needed to play all the rounds. In this case until round 6 int
* currentRoundNumer = 1 -> it means that the round should start at round 1
* currentRoundNumer < totalNumberOfRounds -> this means that the process inside
* the loop will be repeatedly executed up until the total number of rounds, in
* this case round 6 currentRoundNumer++ -> this means that we should increment
* the currentRoundNumber after every round
*/
for (int currentRoundNumber = 1; currentRoundNumber <= totalNumberOfRounds; currentRoundNumber++) {
// I am assuming that player1 will take his/her turn first and then followed by
// player2's turn
System.out.println("====== Start of round " + currentRoundNumber + " ====== ");
int player1RoundScore = playOneRound(currentRoundNumber, player1); // This will generate player1's turn
player1AllScore[currentRoundNumber - 1] = player1RoundScore; // This will save the score of player 1 in this
// round. As you can see I decremented 1 to
// the current round number, it is because
// the array index starts at 0
int player2RoundScore = playOneRound(currentRoundNumber, player2); // This will generate player2's turn
player2AllScore[currentRoundNumber - 1] = player2RoundScore; // This will save the score of player 2 in this
// round. As you can see I decremented 1 to
// the current round number, it is because
// the array index starts at 0
System.out.println("====== End of round " + currentRoundNumber + " ====== \n");
}
/*
* You can do some calculations here regarding the scores of each player. To get
* the score you can do this: player1AllScore[0]; The above code will get the
* round 1 score of player 1. Do not forget that you should decrement 1 in each
* round. so if you want to get the score of player 1 on round 2, you will pass
* '1', thus it should be player1AllScore[1];
*/
}
public static int diceRoll() {
/*
* The lines that are deleted below is correct, but since you do not use roll
* variable in any other process, you can just directly return the generated
* random, number
*/
// int roll;
// roll = (int) (Math.random() * 6 + 1);
// return roll;
return (int) (Math.random() * 6 + 1);
}
/*
* This will be the method that will calculate the scores of a player Since you
* are using roundNumber from your previous code, I am assuming that you are
* calculating the score in each round
*
* @param roundNumber The current round
*
* @param dice1 The value of dice 1
*
* @param dice2 The value of dice 2
*
* @param dice3 The value of dice 3
*/
public static int getScore(int roundNumber, int dice1, int dice2, int dice3) {
// diceRoll(); <- you do not need this since you are not rolling the dice, you
// are just about to calculate the score
// int roundNumber; <- you do not need this since it is already passed as a
// parameter
/*
* Starting from this point I do not actually understand what you are trying to
* do so I have just based in on assumptions.
*/
// [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;
int score = 0; // initialize the score
/*
* just my assumption since from the code above, I think when all the dice
* values are equal to the round number you will have a bonus points? If so then
* the above condition will satisfy it
*/
if (dice1 == roundNumber && dice2 == roundNumber && dice3 == roundNumber) {
score = 1000; // you do not need to use a variable "bunco", you can directly assign a value.
}
/*
* just my assumption since from the code above, I think when only dice1 and
* dice2 values are equal to the round number you will have a two points? If so
* then the above condition will satisfy it
*/
else if (dice1 == roundNumber && dice2 == roundNumber) {
score = 2;
}
/*
* just my assumption since from the code above, I think when only dice1 values
* are equal to the round number you will have a one point? If so then the above
* condition will satisfy it
*/
else if (dice1 == roundNumber) {
score = 1;
}
return score;
}
/*
* This will be the method that will do 1 round. It will roll the 3 dice and
* calculate the score
*
* @param roundNumber The current round
*
* @param player The name of the player who is playing this round
*/
public static int playOneRound(int roundNumber, String player) {
// diceRoll(); <- you do not need this
int die1 = diceRoll();
int die2 = diceRoll();
int die3 = diceRoll();
// getScore(); <- you do not need this
int score = getScore(roundNumber, die1, die2, die3);
// String player; <- you do not need this
// Since this methods goal is to play 1 round you do not have to loop this
// for (int roundNumber = 1; roundNumber <= 3; roundNumber++) {
System.out.println(player + " rolled " + die1 + "," + die2 + "," + die3 + " and scored " + score + " points");
// }
// return the score for calculating the total score for all the rounds
return score;
}
}
/**
*
*
*/
公共班机{
/**
*@param args
*/
公共静态void main(字符串[]args){
/*
*由于不需要,下面的行已被删除。您的游戏只是
*马上就要开始了,不用掷骰子就可以得分了
*/
//骰子卷();
//getScore();
String player1=args[0];//删除了不必要的括号。以前是(args[0])
String player2=args[1];//删除了不必要的括号。以前是(args[1])
playBunco(player1,player2);//将player1和player2变量传递给playBunco,以便playBunco方法
//将有权接触球员
}
/*
*这将是处理“播放”部分的方法
*
*@param player1玩家1的名字
*
*@param player2玩家2的名字
*/
公共静态无效playBunco(字符串播放器1、字符串播放器2){
int player1AllScore[]={0,0,0,0,0,0};//用于存储玩家1的分数。它有六个0
//自
//你有6发子弹
int player2AllScore[]={0,0,0,0,0,0};//用于存储玩家1的分数。它有六个0
//自
//你有6发子弹
int totalNumberOfRounds=6;//这是总轮数
/*
*所有回合都需要这个循环。在这种情况下,直到第6回合结束
*currentRoundNumer=1->表示该轮应从第1轮开始
*currentRoundNumer这意味着
*循环将重复执行,直到达到总循环次数(单位:秒)
*本例为第6轮currentRoundNumer++->这意味着我们应该增加
*每轮后的currentRoundNumber
*/
对于(int CurrnCouthNoint=1;CurrCurrnActudio),是否需要上一轮信息,比如上一轮用户在哪里滚动?或者只是点已经足够了?欢迎@ MARE。您考虑使用数组(INT)吗?要在两次掷骰之间存储骰子结果?@Ravi-我只需要上一次掷骰的点数round@TungstenX-我没有,但只是因为我还没有真正学会。不过我会查找并尝试!我肯定需要了解更多,这真的很有帮助。我将尝试修改我的整个程序以反映您的代码。非常感谢您的帮助你的解释!!@Mare我很高兴我帮助了你。别忘了从基础开始。:)
}
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
}
/**
*
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
/*
* The lines below are deleted since they are not needed. Your game is just
* about to start. No need to roll the dice and get the score
*/
// diceRoll();
// getScore();
String player1 = args[0]; // Deleted the unnecessary parenthesis. Previously was (args[0])
String player2 = args[1]; // Deleted the unnecessary parenthesis. Previously was (args[1])
playBunco(player1, player2); // Pass the player1 and player2 variables to playBunco, so that playBunco method
// will have access to the players
}
/*
* This will be the method that the "play" part will be processed
*
* @param player1 The name of player 1
*
* @param player2 The name of player 2
*/
public static void playBunco(String player1, String player2) {
int player1AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's
// since
// you have 6 rounds
int player2AllScore[] = { 0, 0, 0, 0, 0, 0 }; // This is used to store the score of player 1. It has six 0's
// since
// you have 6 rounds
int totalNumberOfRounds = 6; // this is the total number of rounds
/*
* This loop is needed to play all the rounds. In this case until round 6 int
* currentRoundNumer = 1 -> it means that the round should start at round 1
* currentRoundNumer < totalNumberOfRounds -> this means that the process inside
* the loop will be repeatedly executed up until the total number of rounds, in
* this case round 6 currentRoundNumer++ -> this means that we should increment
* the currentRoundNumber after every round
*/
for (int currentRoundNumber = 1; currentRoundNumber <= totalNumberOfRounds; currentRoundNumber++) {
// I am assuming that player1 will take his/her turn first and then followed by
// player2's turn
System.out.println("====== Start of round " + currentRoundNumber + " ====== ");
int player1RoundScore = playOneRound(currentRoundNumber, player1); // This will generate player1's turn
player1AllScore[currentRoundNumber - 1] = player1RoundScore; // This will save the score of player 1 in this
// round. As you can see I decremented 1 to
// the current round number, it is because
// the array index starts at 0
int player2RoundScore = playOneRound(currentRoundNumber, player2); // This will generate player2's turn
player2AllScore[currentRoundNumber - 1] = player2RoundScore; // This will save the score of player 2 in this
// round. As you can see I decremented 1 to
// the current round number, it is because
// the array index starts at 0
System.out.println("====== End of round " + currentRoundNumber + " ====== \n");
}
/*
* You can do some calculations here regarding the scores of each player. To get
* the score you can do this: player1AllScore[0]; The above code will get the
* round 1 score of player 1. Do not forget that you should decrement 1 in each
* round. so if you want to get the score of player 1 on round 2, you will pass
* '1', thus it should be player1AllScore[1];
*/
}
public static int diceRoll() {
/*
* The lines that are deleted below is correct, but since you do not use roll
* variable in any other process, you can just directly return the generated
* random, number
*/
// int roll;
// roll = (int) (Math.random() * 6 + 1);
// return roll;
return (int) (Math.random() * 6 + 1);
}
/*
* This will be the method that will calculate the scores of a player Since you
* are using roundNumber from your previous code, I am assuming that you are
* calculating the score in each round
*
* @param roundNumber The current round
*
* @param dice1 The value of dice 1
*
* @param dice2 The value of dice 2
*
* @param dice3 The value of dice 3
*/
public static int getScore(int roundNumber, int dice1, int dice2, int dice3) {
// diceRoll(); <- you do not need this since you are not rolling the dice, you
// are just about to calculate the score
// int roundNumber; <- you do not need this since it is already passed as a
// parameter
/*
* Starting from this point I do not actually understand what you are trying to
* do so I have just based in on assumptions.
*/
// [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;
int score = 0; // initialize the score
/*
* just my assumption since from the code above, I think when all the dice
* values are equal to the round number you will have a bonus points? If so then
* the above condition will satisfy it
*/
if (dice1 == roundNumber && dice2 == roundNumber && dice3 == roundNumber) {
score = 1000; // you do not need to use a variable "bunco", you can directly assign a value.
}
/*
* just my assumption since from the code above, I think when only dice1 and
* dice2 values are equal to the round number you will have a two points? If so
* then the above condition will satisfy it
*/
else if (dice1 == roundNumber && dice2 == roundNumber) {
score = 2;
}
/*
* just my assumption since from the code above, I think when only dice1 values
* are equal to the round number you will have a one point? If so then the above
* condition will satisfy it
*/
else if (dice1 == roundNumber) {
score = 1;
}
return score;
}
/*
* This will be the method that will do 1 round. It will roll the 3 dice and
* calculate the score
*
* @param roundNumber The current round
*
* @param player The name of the player who is playing this round
*/
public static int playOneRound(int roundNumber, String player) {
// diceRoll(); <- you do not need this
int die1 = diceRoll();
int die2 = diceRoll();
int die3 = diceRoll();
// getScore(); <- you do not need this
int score = getScore(roundNumber, die1, die2, die3);
// String player; <- you do not need this
// Since this methods goal is to play 1 round you do not have to loop this
// for (int roundNumber = 1; roundNumber <= 3; roundNumber++) {
System.out.println(player + " rolled " + die1 + "," + die2 + "," + die3 + " and scored " + score + " points");
// }
// return the score for calculating the total score for all the rounds
return score;
}
}