2017-03-06 8 views
-4

の私のロジックで、私はプログラムを働いていると私は確信していただきました!私のプログラムで間違ってないんだけど、すべての罰金を動作するようですが、私はプログラムを実行するために行くとき、それは停止しません。無限ループがコード

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

を使用すると、2つのwhileループを持っていますなぜですか? 1つはサイコロ!= -1と1つはサイコロ<=トライ? –

+0

プログラム全体でdo-while(カウンタ== 0)をプラスします。余分なループをすべて取り除く。 –

+0

idk imはプログラムを動作させようとしていますが、2つのwhileループは悪いですか? – GeneralWolf

答えて

1

ダイスの値が決して-1にならないため、無限大です。 2つのループを結合する。

カウンタは常にゼロになりますので、それはまだ変数に対抗し続けるかどうかの入力をリダイレクトし、無限になります。リファクタリング、コード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); 
} 
+0

コードdoesntの仕事 – GeneralWolf

+0

は何を働いていない//stackoverflow.com/help/someone-answers?それはまだ無限ループですか?編集したコードを取ってください。私も編集してテストしました。質問に答えた場合は、回答としてマークしてください。 –

+0

コードが機能しています。 Upvotedそれ。 'もし((response_2!= NULL)&&!response_2.isEmpty())' '前parseDouble'と' parseInt'を適用することが提案 – c0der

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