我使用两个文件在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!");
}
}
}
这个
import java.util.Random; //class used to generate random number for dice roll
public class Dice {
private int numberSides;
private Random randomGenerator;
private int currentRoll;
//default constructor
Dice() {
randomGenerator = new Random(); //initialize random object
numberSides = 6; //default number of sides
currentRoll = randomGenerator.nextInt(numberSides)+1; //initialize roll (1-6)
}
public int getRoll() {
return currentRoll;
}
//"roll" a random integer between 1 and numberSides
public void roll() {
currentRoll = randomGenerator.nextInt(numberSides)+1; //reroll 1-6
}
}
答案 0 :(得分:2)
现在你只需在循环开始时调用die1.getRoll。除非你给roll打电话,否则这个数字不会改变。
从构造函数中删除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
或者,您可以在游戏中进行一些修改并更改功能。
public int getRoll()
{
roll();
return currentRoll;
}
答案 1 :(得分:1)
为什么我的Java骰子游戏不断重复推动?
你不断获得相同的随机数,因为唯一负责生成新随机数的代码位于Dice类的构造函数中。
构造函数只会在实例化时调用一次。随后致电componentDidMount(){
console.log(this.props) // Never fired..why??
let {dish, ingredients} = this.props.content
this.setState({
dish:dish, ingredients:ingredients
})
}
将只返回相同的随机数。
如果您想从getRoll()
收到新的随机数,可以这样做:
getRoll()
如果您希望Dice类“记住”当前的滚动,您可以使用以下方法:
public int getRoll(){ //return a new dice roll every time
return (randomGenerator.nextInt(numberSides)+1);
}
我如何调用roll()函数getRoll()函数?你能说明一下吗?
您不需要public int roll(){ //return a new dice roll every time & save current
currentRoll = randomGenerator.nextInt(numberSides)+1;
return currentRoll;
}
和roll()
,任何一个都足以生成新的随机数。您只需确保在getRoll()
或randomGenerator.nextInt(numberSides)+1
方法中放置roll()
即可使其正常工作。