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

Java 代码中逻辑中的无限循环,java,computer-science,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

我正在运行一个程序,我不确定我的程序出了什么问题,它似乎运行得很好,但当我运行程序时,它不会停止

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

它是无限的,因为骰子值永远不会是-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);
}

请检查并询问是否不清楚:

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

为什么有两个while循环?一个骰子!=-1和1表示骰子加上整个程序的do whilecounter==0。摆脱所有多余的循环。idk im仅尝试使程序工作对两个while循环不好吗?你想将它们组合成一个while骰子!=1&骰子骰子永远不会变成-1。那么,第二个while循环永远有什么不起作用?它仍然在循环中吗?请使用经过编辑的代码。我也进行了编辑和测试。如果回答了您的问题,请将其标记为已回答。代码正在工作。投票支持它。建议应用IFU 2!=空&!在parseDouble和parseInt之前,response_2.isEmpty