为什么我的Java骰子游戏总是重复掷骰子?

为什么我的Java骰子游戏总是重复掷骰子?,java,Java,我用两个文件在Java中制作了一个骰子游戏。代码运行得很好,但似乎有一个逻辑错误,我不明白。在游戏中,它只输出与上一次掷骰相同的值。所以如果骰子掷出一个6,你再掷一次,它会说你又连续掷出一个6。我目前正在尝试修复它,但遇到了问题。任何帮助都将不胜感激。 以下是两个程序: import java.util.Scanner; public class DiceGameTest { static Scanner input = new Scanner(System.in); pu

我用两个文件在Java中制作了一个骰子游戏。代码运行得很好,但似乎有一个逻辑错误,我不明白。在游戏中,它只输出与上一次掷骰相同的值。所以如果骰子掷出一个6,你再掷一次,它会说你又连续掷出一个6。我目前正在尝试修复它,但遇到了问题。任何帮助都将不胜感激。 以下是两个程序:

import java.util.Scanner;

public class DiceGameTest {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        //declare instance variables
        int choice = 0;
        //int total;
        //total = die1.getRoll() + die2.getRoll();

        //create the 2 die
        Dice die1 = new Dice();
        //Dice die2 = new Dice();

        //print out description of game
        System.out.println("Welcome to Eric and John's Dice game!");
        System.out.println("This dice game is very simple, here are the rules!");
        System.out.println("    1. Roll a die");
        System.out.println("    2. To win, you must get a 4 or higher");
        System.out.println("    3. Have Fun!\n");

        //ask the user if they want to roll the dice or quit
        System.out.println("Would you like to roll the die to start playing? Press 1 to roll or \"-1\" to quit");
        //user's choice
        choice = input.nextInt();

        //if the user puts 1
        if(choice == 1)
        {
            System.out.printf("You rolled a %d%n", die1.getRoll());
        }

        //play the game
        do
        {
            die1.getRoll();
            if(die1.getRoll() >= 4)
            {
                System.out.println("Hooray! You won by getting a: " + die1.getRoll());
            }
            else if(die1.getRoll() < 4)
                {
                    System.out.println("Too Bad! Your roll was: " + die1.getRoll() + " and it was not greater than or equal to 4");
                }
            //ask the user if they want to roll the dice again or quit
            System.out.println("Would you like to roll the die to start playing? Press 1 to roll or \"-1\" to quit");
            //user's choice
            choice = input.nextInt();

        }while(choice != -1);

        if(choice == -1)
        {
            System.out.println("You Quit the Game!");
        }
    }
}

现在,您只需在循环开始时继续调用die1.getRoll。除非你们点名,否则那个号码不会改变

从构造函数中删除currentRoll。你不需要把它放在那里。那么

die1.getRoll()
应该是,

die1.roll()
在您的do-while循环中,如下所示

  do
    {
        die1.roll();
        if(die1.getRoll() >= 4)
        {
            System.out.println("Hooray! You won by getting a: " +     die1.getRoll());
        }
        else if(die1.getRoll() < 4)
        //rest of it
为什么我的Java骰子游戏总是重复掷骰子

您不断得到相同的随机数,因为负责生成新随机数的唯一代码位于Dice类的构造函数中

构造函数在实例化时只调用一次。随后调用
getRoll()
只会返回相同的随机数

如果您想从
getRoll()
接收新的随机数,可以按以下方式执行:

public int getRoll(){    //return a new dice roll every time
    return (randomGenerator.nextInt(numberSides)+1);
}
如果您希望骰子类“记住”当前掷骰,可以使用如下方法:

public int roll(){    //return a new dice roll every time & save current
    currentRoll =  randomGenerator.nextInt(numberSides)+1;
    return currentRoll;
}

如何将roll()函数调用为getRoll()函数?你能具体说明一下吗


您不需要同时使用
roll()
getRoll()
,任何一个都足以生成新的随机数。您只需确保在
roll()
getRoll()
方法中放置
randomGenerator.nextInt(numberSides)+1
,即可使其工作。

您何时滚动?按照当前的方式,您的骰子仅在创建时以及在函数roll()中为其滚动变量分配随机值(我认为它从未被调用过)。当你可以调用一个getRoll()函数时,你可以调用roll()函数,或者在每次玩家滚动时创建一个新的单独骰子(不建议)。我该如何将roll()函数称为getRoll()函数?你能指定吗?只需添加行“roll();”(无引号)在getRoll()函数中返回之前,应该这样做。我担心您编写它的方式太混乱了。他所要做的就是返回在方法
roll()
中生成的值,而不是将其分配给变量,然后将方法调用从
getRoll()
交换到
roll()
在他的循环中。另外,我建议OP不要多次调用该方法,而是将其分配给循环中的一个变量并使用它。我不认为这有多混乱。我在编辑答案之前写了评论。现在已经更好了。我建议使用另一种方法的唯一原因是,如果你有一个死,你实际上已经死了所以为什么在构造函数中“滚动”它,然后再滚动它。更直观的说法是die.roll()滚动它,然后die.getroll()获得滚动。这就是为什么我这样说的原因当然,我一点也不反对。我只需完全删除方法
getroll()
,然后让方法
roll())
直接返回结果。但这只是一个值得争论的问题,你的答案是正确的。@BeenisProgramming看看我的解决方案
public int getRoll(){    //return a new dice roll every time
    return (randomGenerator.nextInt(numberSides)+1);
}
public int roll(){    //return a new dice roll every time & save current
    currentRoll =  randomGenerator.nextInt(numberSides)+1;
    return currentRoll;
}