【发布时间】:2013-03-05 06:44:18
【问题描述】:
我的程序目标是首先从 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();
}
}
}
}
【问题讨论】:
-
如果您希望我们帮助您解决此问题,我们将不胜感激。
-
对不起,我刚刚粘贴了它,我很抱歉!
-
您在哪里分配
exit值?你的 while 循环永远不会退出。 -
System.out.println("The roll is : " + nextRoll);?您可能希望始终这样做,而不仅仅是在else内。 -
您是否尝试过使用调试器?使用控制台打印语句可能无法正确告诉您程序中发生了什么,因为 System.In 不是立即打印的。尝试使用调试器并评估您的表达式。