2016-11-12 71 views
0

提示:玩家選擇一個範圍(最小和最大),然後考慮該範圍內的數字(不需要在程序中輸入數字)。遊戲應該使用二進制搜索來系統地猜測玩家的號碼。玩家應該在回合之間告訴計算機「太高」或「太低」或「正確」。程序應該繼續,直到電腦得到答案,或檢測到作弊(或肯定知道答案)。在退出之前,計算機應該說出它有多少「輪」(需要多少次猜測)。如何找到使用二進制搜索的用戶號碼

問題:電腦是錯後的第一時間和用戶聲明太高或太低,我不能ressign值的上限和下限範圍

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'最大值和最小值?他們完成第一輪之後? – Searching

+0

仍然無法使用? – Searching

回答

1

您將需要使用scan.nextLine()而不是scan.next(),讀取包含space個字符的行中的所有內容,這就是爲什麼max和min永遠不會放在首位的原因。

掃描程序使用定界符模式將其輸入分爲標記,默認情況下該定界符與空白匹配。

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 {}'時使用更習慣的''。這不會讓讀者不確定破壞情況。 –