代码中我的逻辑中的无限循环

时间:2017-03-06 02:05:28

标签: java computer-science

我正在制作一个程序而且我不确定我的程序有什么问题,这一切似乎都很好但是当我去运行程序时,它并没有停止。

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);
 }
}

2 个答案:

答案 0 :(得分:1)

它无限,因为骰子值永远不会是-1。将两个循环组合在一起。

它仍然是无限的,因为计数器总是为零,将continue或not的输入重定向到计数器变量。

PFB重构代码。

 public static void main(String[] args)
{
    int counter = 1;
    do{
        String response = JOptionPane.showInputDialog(null, "Please enter the number of tries: ");
        final int TRIES = Integer.parseInt(response);
        int dice = 1;
        double dice_2 = 0;
        while(dice_2 != -1 || 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);
                dice_2 = Double.parseDouble(response_2);
            }
        counter = JOptionPane.showConfirmDialog(null, "Would you like to run it again? ", "Dice Generator" , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        System.out.println(counter);
    }while(counter == 0);
}

答案 1 :(得分:0)

请查看,并询问是否不清楚:

    public static void main(String[] args)
    {
        int stop = 1; 
        while(stop != 0) //loop to control re-runs 
        {
            String response = JOptionPane.showInputDialog(null, "Please enter the number of tries: ");
            final int TRIES = Integer.parseInt(response);
            int dice = 1;
            while((dice <= TRIES) && (stop != 0)) //loop to control re-tries 
            {
                int d1 = (int) (Math.random() * 6) + 1;
                int d2 = (int) (Math.random() * 6) + 1;
                dice++;

                stop = JOptionPane.showConfirmDialog(null, d1 + "   " + d2 +"\n Quit game? ", "Dice Generator" , JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            }
        }
     }