繁体   English   中英

Java中的随机,重复相同的数字

[英]Random in Java, repeating same number

我的代码:

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;
    }

上面的代码是针对掷骰子游戏的,我的问题是用户何时需要获得新点数。 他们没有分配新的随机数,而是收到与以前相同的数字。 有没有办法调用 RandomNumber 类,并获得一个新的随机数来分配给newPoint变量?

谢谢

RandomNumber类中添加一个方法来生成一个新的 Roll。

class RandomNumber{
     Random rand = new Random();
     int randNum = rand.nextInt(12) + 1;
     void Roll(){
        randNum = rand.nextInt(12) + 1;
     }
}

然后在下一步之前添加对Roll()的调用。

//Roll dice again for the new point
diceRoll.Roll();
playGame.newPoint = diceRoll.randNum;

附加这一行

`RandomNumber diceRoll = new RandomNumber();`

就在之前

`playGame.newPoint = diceRoll.randNum;`

因为您始终使用相同的对象和相同的 randNum 属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM