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

时间:2019-02-26 23:08:05

标签: java random duplicates

我的代码:

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

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

谢谢

2 个答案:

答案 0 :(得分:0)

rp("URI") .then(data =>{ res.contentType("application/pdf") res.send(data) }) .catch(e =>{ console.log(e) }) 类添加方法以生成新的Roll。

RandomNumber

然后在下一步之前添加对class RandomNumber{ Random rand = new Random(); int randNum = rand.nextInt(12) + 1; void Roll(){ randNum = rand.nextInt(12) + 1; } } 的呼叫。

Roll()

答案 1 :(得分:0)

追加此行

`RandomNumber diceRoll = new RandomNumber();`

就在

之前
`playGame.newPoint = diceRoll.randNum;`

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