【发布时间】:2017-03-06 02:05:27
【问题描述】:
我正在运行一个程序,但我不确定我的程序有什么问题,一切似乎都很好,但是当我运行该程序时,它并没有停止。
import javax.swing.JOptionPane;
public class Random_Numbers_2
{
public static void main(String[] args)
{
int counter = 0;
do{
String response = JOptionPane.showInputDialog(null, "Please enter the number of tries: ");
final int TRIES = Integer.parseInt(response);
int dice = 1;
while(dice != -1)
{
while(dice <= TRIES)
{
int d1 = (int) (Math.random() * 6) + 1;
int d2 = (int) (Math.random() * 6) + 1;
dice++;
String response_2 = JOptionPane.showInputDialog(null, d1 + " " + d2 + "\n" + "Enter any number to continue, it will not effect the program" + "\n" + "Please enter -1 when doubles show", "Dice Generator" , JOptionPane.INFORMATION_MESSAGE);
double dice_2 = Double.parseDouble(response_2);
}
}
JOptionPane.showConfirmDialog(null, "Would you like to run it again? ", "Dice Generator" , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
}while(counter == 0);
}
}
【问题讨论】:
-
为什么有两个while循环?一个用于骰子!= -1,一个用于骰子
-
加上整个程序的do-while(counter==0)。摆脱所有额外的循环。
-
idk 我只是想让程序工作对两个 while 循环不利?
-
假设两个表达式的逻辑是正确的,您想将它们组合成一个 while (dice != 1 && dice
-
dice永远不会变成 -1。所以,第二个 while 永远循环
标签: java computer-science