【问题标题】:Infinite loop in my logic in code我的代码逻辑中的无限循环
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 什么没用?它仍然是无限循环吗 - 请使用编辑过的代码。我也进行了编辑和测试。如果它回答了您的问题,请标记为已回答。
  • 代码正在运行。赞成它。建议在parseDoubleparseInt之前申请if((response_2 != null) &amp;&amp; ! response_2.isEmpty())
【解决方案2】:

请复习,如有不明白请追问:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2015-07-14
    • 2012-03-15
    • 2017-03-09
    相关资源
    最近更新 更多