2016-05-04 7 views
0

私は数を絶えず推測し、数字が高すぎるか低すぎるかをユーザーに尋ね、正しい数字を推測するまで推測を調整しようとしています。ユーザーはプログラムが正しい番号を見つけたと言います。次に、数を推測するのに何回かを返します。カスタムボタンを使って数字を推測するゲームを作る

私の問題は、私はのJOptionPaneで推測

public static void main(String[] args) { 

    int result; 
    String setUpper = JOptionPane.showInputDialog("Please set the upper bound"); 
    Scanner input; 
    input = new Scanner(setUpper); 
    int upperBound; 
    upperBound = input.nextInt(); 
    int [] guessArray = new int[upperBound]; 

    int left; // Left edge of the unsearched portion 
    int right; // just past right edge of unsearched portion 
    left = 0; 
    right = guessArray.length; 
    counter = 0; 


    JOptionPane.showMessageDialog(null, "Alright, I'll find the number you picked, from 0 to " + guessArray.length, "Output", JOptionPane.INFORMATION_MESSAGE); 

    Object[] options = {"Too low", "You got it!", "Too high"}; 

    result = JOptionPane.showOptionDialog(null, "Is your number " + (guessArray.length/2), "Guess", 
     JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, 
     null, options, options[0]); 

    while (left != right-1) { // while left, right not adjacent 
     int mid = (right+left)/2; 
     counter++; 
     //guessArray[mid]; 

     if (result == JOptionPane.YES_OPTION) { // it's to the left 
      right = mid; 

     } 
     else { 
      if (result == JOptionPane.CANCEL_OPTION) { 
       left = mid; 
      } 
      else { 
       if (result == JOptionPane.NO_OPTION) { 
        JOptionPane.showMessageDialog(null, "It took " + counter + " comparisons to find your number", 
         "Output", JOptionPane.INFORMATION_MESSAGE); 
       } 

      } 

     } 
    } 

} 

答えて

0

を調整するための別の方法に私の別のカスタムボタンを接続するかどうかはわかりませんということです、ボタンにメソッドをバインドすることはできません。戻り値が返され、ユーザー操作が表示されるまで呼び出し元がブロックされます。 Java-Doc

あなたはJavaFXの、JavaFXの-Buttonクラスを見てしたい場合は、通常のJButtonがを使用したりすることができ、ボタンにメソッドをバインドする:

は詳細についてはマニュアルを参照してください。これは、コンポーネントのイベントをリッスンすることによって行われます。良い解決策は、ラベルやテキストフィールドにプログラムの質問と回答のメッセージを表示し、その下に「はい」と「いいえ」ボタンを置くことです。このボタンをクリックすると、あなたの行動が実行されます。

0

3つのボタンを定義している場合、JOptionPaneは0,1または2を返します。

あなたのコードはコンパイルされません。コンパイルエラーを修正した後、次の数値を計算するためのボタンコードは後方にありました。最後に、JOptionPaneコードをdo whileループの中に入れて、人がJOptionPaneを2回以上見たようにしました。

ここに修正コードがあります。私があなたのコードに対して何をしたかを理解して理解する時間を取ってください。

package com.ggl.testing; 

import java.util.Scanner; 

import javax.swing.JOptionPane; 

public class NumberGuessing { 

    public static void main(String[] args) { 

     int result; 
     String setUpper = JOptionPane 
       .showInputDialog("Please set the upper bound"); 
     Scanner input; 
     input = new Scanner(setUpper); 
     int upperBound; 
     upperBound = input.nextInt(); 
     int[] guessArray = new int[upperBound]; 

     int left; // Left edge of the unsearched portion 
     int right; // just past right edge of unsearched portion 
     left = 0; 
     right = guessArray.length; 
     int counter = 0; 
     int mid = guessArray.length/2; 

     input.close(); 

     JOptionPane.showMessageDialog(null, 
       "Alright, I'll find the number you picked, from 0 to " 
         + (guessArray.length - 1), "Output", 
       JOptionPane.INFORMATION_MESSAGE); 

     Object[] options = { "Too low", "You got it!", "Too high" }; 

     do { // while not correct number 

      result = JOptionPane.showOptionDialog(null, 
        "Is your number " + mid, "Guess", 
        JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, 
        null, options, options[0]); 

      // result is zero, number is larger 
      if (result == JOptionPane.YES_OPTION) { 
       left = mid; 
       mid = (right + left)/2; 
       counter++; 
       // result is 2, number is smaller 
      } else if (result == JOptionPane.CANCEL_OPTION) { 
       right = mid; 
       mid = (right + left)/2; 
       counter++; 
      } 

     } while (result != 1); 

     JOptionPane.showMessageDialog(null, "It took " + counter 
       + " comparisons to find the number " + mid, "Output", 
       JOptionPane.INFORMATION_MESSAGE); 

    } 

} 
関連する問題