我的程序目标是首先从 1-6 生成一个随机数,称为“点”,然后用户将继续输入一个键来重新掷出相应的“骰子”,如果掷出相同的数字,用户应该提示来自第一个 if 语句的消息
但是,无论何时掷下一个骰子,它都不会滚到点数上,并且第一个 if 语句打印行会随机打印出来。关于如何解决这个问题的答案将不胜感激?
import java.io.*;
public class DiceGame1
{
public static void main (String [] args) throws IOException
{
String userInput;
int DiceRoll;
int exit = 0;
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Hello, this program rolls a dice and outputs the number rolled");
System.out.println("The number rolled is called the point it will keep rolling untill it");
System.out.println("hits that point again, press any key to con't");
userInput = myInput.readLine();
DiceRoll = (int) (Math.random () *(6-1) + 1);
System.out.println("The point to match is: " + DiceRoll);
System.out.println("Press any key to roll again...");
userInput = myInput.readLine();
while(exit != 999) {
int nextRoll = (int) (Math.random () *(6-1) + 1);
if (nextRoll == DiceRoll)
{
System.out.println(" It took the computer _____ tries to reach the point");
}
else
{
System.out.println("The roll is : " + nextRoll);
System.out.println("Press any key to roll again...Or press '999' to exit");
userInput = myInput.readLine();
}
}
}
}