[英]Random in Java, repeating same number
My code:我的代码:
public class SkillsDemoTwoCrapsGameClass {
public static void main(String[] args) {
RandomNumber diceRoll = new RandomNumber(); //Create instance diceRoll of class RandomNumber
play playGame = new play(); //Create instance playGame of class play
//Initialise variables from play class
playGame.diceRoll = diceRoll.randNum;
playGame.point = playGame.diceRoll;
playGame.newPoint = diceRoll.randNum;
System.out.println("WELCOME TO A GAME OF CRAPS!");
if(playGame.diceRoll == 7 || playGame.diceRoll == 11){
//Show the users point
System.out.println("Point: " + playGame.point);
System.out.println("------------------------------");
//Tell user they won
System.out.println("Congratulations you won, with a " + playGame.diceRoll);
}
else if(playGame.diceRoll == 2 || playGame.diceRoll == 3|| playGame.diceRoll == 12){
//Show the users point
System.out.println("Point: " + playGame.point);
System.out.println("------------------------------");
//Tell the user they lost
System.out.println("Sorry you lost, with a " + playGame.diceRoll);
}
else{
System.out.println("Point: " + playGame.point);
System.out.println("------------------------------");
while(playGame.point != playGame.newPoint || playGame.point == playGame.newPoint){
/*
BUG: (2/2/19)
User will receive their original roll again, causing them to always win
*/
//Roll dice again for the new point
playGame.newPoint = diceRoll.randNum;
//Checks if the user can win
if(playGame.point == playGame.newPoint){
System.out.println("Your new roll: " + playGame.newPoint + "\t\t Win");
break;
}
//Checks if the user has lost
else if(playGame.newPoint == 7){
System.out.println("Your new roll: " + playGame.newPoint + "\t\t Lose");
break;
}
//Check if user needs to roll again
else{
System.out.println("Your new roll: " + playGame.newPoint + "\t\t No help");
}
}
}
}
}
class RandomNumber{
Random rand = new Random();
int randNum = rand.nextInt(12) + 1;
}
class play{
int diceRoll, point, newPoint;
}
The above code is for a game of Craps, my problem is when the user needs to get a new point.上面的代码是针对掷骰子游戏的,我的问题是用户何时需要获得新点数。 Instead of a new random number being assigned, they receive the same number as before.
他们没有分配新的随机数,而是收到与以前相同的数字。 Is there a way to call the RandomNumber class, and get a new random number to assign to the
newPoint
variable?有没有办法调用 RandomNumber 类,并获得一个新的随机数来分配给
newPoint
变量?
Thanks谢谢
Add a method to the RandomNumber
class to generate a new Roll.在
RandomNumber
类中添加一个方法来生成一个新的 Roll。
class RandomNumber{
Random rand = new Random();
int randNum = rand.nextInt(12) + 1;
void Roll(){
randNum = rand.nextInt(12) + 1;
}
}
Then add a call to Roll()
before the next move.然后在下一步之前添加对
Roll()
的调用。
//Roll dice again for the new point
diceRoll.Roll();
playGame.newPoint = diceRoll.randNum;
Append this line附加这一行
`RandomNumber diceRoll = new RandomNumber();`
just before就在之前
`playGame.newPoint = diceRoll.randNum;`
Because you use always the same object and same randNum attribute因为您始终使用相同的对象和相同的 randNum 属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.