2016-11-12 7 views
0

プロンプト:プレーヤーは範囲を(最小値と最大値の両方を)選択し、その範囲の数値を考えます(プログラムに数値を入力する必要はありません)。ゲームでは、プレーヤーの番号を体系的に推測するためにバイナリ検索を使用する必要があります。プレーヤーは、ラウンド間でコンピュータに「高すぎる」または「低すぎる」または「正しい」と伝える必要があります。プログラムは、コンピュータが答えを得るまで、または不正行為を検出するまで(または、確かに答えを知っているまで)続けなければなりません。終了する前に、コンピュータは、それがどれくらいの「ラウンド」であったか(何回の推測が行われたか)を述べるべきである。バイナリ検索を使用してユーザ番号を見つけるにはどうすればいいですか?

問題:コンピュータが初めて間違っていると、ユーザーが高すぎたり、低すぎたり宣言した後、私はあなたが使用する必要があります上下の範囲

import java.util.Scanner; 

public class TestPractice { 

    public static void main(String[] args) { 
     System.out.println("Think of a number"); 
     Scanner scan = new Scanner(System.in); 
     String x = null; 
     String y = null; 
     String i = null; 
     //Get the input from the player 
     System.out.println("Please your maximum value"); 

     if (scan.hasNext()) { 
      x = scan.next(); 
     } 

     System.out.println("Please input your min value"); 
     if (scan.hasNext()) { 
      y = scan.next(); 
     } 

     //Parse the input so its usuable in the array 
     int max = Integer.parseInt(x); 
     int min = Integer.parseInt(y); 

     boolean numberguessed = true; 
     int numberofRounds = 0; 

     while(numberguessed) { 
      int midpoint = (max+min)/2; 

      numberofRounds++; 

      System.out.println("Is your number " + midpoint + " please say too low or too high or correct"); 
      if (scan.hasNext()) { 
       i = scan.next(); 
      } 
      if (i.equalsIgnoreCase("too high")) { 
       min = midpoint; 
      } 
      if (i.equalsIgnoreCase("too low")) { 
       max = midpoint; 
       min = 0; 
      } 
      if (i.equalsIgnoreCase("correct")) { 
       System.out.println("the number of rounds in this game is" + numberofRounds); 
       break; 
      } 

     } 

    } 
} 
+0

「x」と「y」のMaxとMinをもう一度入力するようにユーザーに指示することを意味しますか?彼らは彼らの最初のラウンドを終えた後? – Searching

+0

まだ動作していませんか? – Searching

答えて

1

の値をressignすることはできませんscan.nextLine()scan.next()の代わりにspace文字を含む行のすべてを読むことができます。これは、最大値と最小値が最初に設定されない理由です。

スキャナは、デフォルトでは空白に一致する区切り文字パターンを使用して、入力をトークンに分割します。ループへ

More info on scanner

再びゲーム全体は、do {} while(true);反復を見てください。

System.out.println("Think of a number"); 
Scanner scan = new Scanner(System.in); 
String playAgain = "y"; 
String x = null; 
String y = null; 
String i = null; 

do { 
    // Get the input from the player 
    System.out.println("Please your maximum value"); 

    if (scan.hasNext()) { 
     x = scan.next(); 
    } 

    System.out.println("Please input your min value"); 
    if (scan.hasNext()) { 
     y = scan.next(); 
    } 

    // Parse the input so its usuable in the array 
    int max = Integer.parseInt(x); 
    int min = Integer.parseInt(y); 
    int midpoint = 0; 
    boolean numberguessed = true; 
    int numberofRounds = 0; 

    while (numberguessed) {   
     midpoint = (max + min)/2; 
     numberofRounds++; 
     System.out.println("Is your number " + midpoint 
       + " please press (l) for too low or (h) for too high or (c) for correct"); 
     if (scan.hasNext()) { 
      i = scan.nextLine(); 
     } 
     System.out.println(i); 
     if (i.equalsIgnoreCase("h")) { 
      min = midpoint; 
     } else if (i.equalsIgnoreCase("l")) { 
      max = midpoint; 
      min = 0; 
     } else if (i.equalsIgnoreCase("c")) { 
      System.out.println("the number of rounds in this game is" 
        + numberofRounds); 
      break; 
     } 

    } 
    System.out.println("Press y to play again"); 
    if (scan.hasNext()) { 
     playAgain = scan.next(); 
    } 
    System.out.println("Game over"); 
} while (playAgain.equalsIgnoreCase("y")); 

More info on do while

の提案ではなく、単語を書くために、ユーザーに尋ねるのH、LとCのような単純なイエス/ノーの答えを使用しないことであろう。我々に教えてください。

+0

'do {} while(true)'の代わりにもっと慣用的な 'while(true)do {}'を使うことをお勧めします。これは読者に壊れた状態を確信させていません。 –

関連する問題