2013-10-18 35 views
-3

該程序在用戶播放時似乎與原始程序一樣(實際上,玩這兩個遊戲的人不應該是能夠告訴他們分開)。 在原始程序中,用戶猜測1到100之間的隨機數。在每次猜測後,程序會返回數字是高於還是低於猜測,直到用戶猜測正確爲止。數字猜測遊戲,強制用戶進行最大數量的猜測(Java)

但是,這個程序作弊。與其選擇1-100範圍內的隨機數字,程序根本不會想到隱藏數字,而是以這樣的方式回答 ,以便強制用戶儘可能多地進行猜測。

我該如何做到這一點?我需要做一個if語句的循環嗎?我現在認爲可以做的唯一事情就是做一些其他的事情,我甚至不知道這是否會起作用。

我應該繼續這個內在的if else的想法,但越來越具體嗎?我只是覺得必須有更好的方法。

UPDATE *

好了,所以我想我已經想通了什麼,我最初來的,但現在的程序卡住在一個無限循環當我選擇與猜測相等的上下選擇一個號碼。任何想法爲什麼?

public static int feedback (Scanner console, int guess, int max) { 
     String sorry = "Sorry, that guess is incorrect."; 
     String lower = "The number I am thinking of is lower."; 
     String higher = "The number I am thinking of is higher."; 
     int guessCount = 0; 
     guess = console.nextInt(); 
     int upperBound = max + 1; 
     int lowerBound = 0; 
     if (guess <= max && guess >= 1) { 
      guessCount = guessCount + 1; 
      if (max - guess < guess) { 
        System.out.println(sorry); 
        System.out.println(lower); 
        System.out.print("Your guess? "); 
        upperBound = guess; 
        guess = console.nextInt(); 
        guessCount = guessCount + 1; 
      } else { 
        System.out.println(sorry); 
        System.out.println(higher); 
        System.out.print("Your guess? "); 
        lowerBound = guess; 
        guess = console.nextInt(); 
        guessCount = guessCount + 1; 

      while (upperBound - lowerBound > 2) { 
       if (guess <= max && guess >= 1) { 
        if ((lowerBound + ((upperBound - lowerBound)/2)) < guess) { 
         System.out.println(sorry); 
         System.out.println(lower); 
         System.out.print("Your guess? "); 
         upperBound = guess; 
         guess = console.nextInt(); 
         guessCount = guessCount + 1; 
        } else { 
          System.out.println(sorry); 
          System.out.println(higher); 
          System.out.print("Your guess? "); 
          lowerBound = guess; 
          guess = console.nextInt(); 
          guessCount = guessCount + 1; 
          } 
       } else { 
        System.out.println("Your guess must be in the range 1-" + max + ". Try again."); 
       } 
        } 
        System.out.println("Yes, the number I was thinking of was " + guess); 
       } 
      } else { 
      System.out.println("Your guess must be in the range 1-" + max + ". Try again."); 
      return guessCount; 
     } 
     return guessCount; 
     } 
+0

從crapton開始! – iluxa

+0

想一下這些限制:任何新的答案都不應該與過去的答案相矛盾。用戶猜測的範圍應該是最大的。然而,對於使用二分搜索的用戶來說,作弊最多會迫使他做log2(n)猜測。 – Tarik

+0

你在這裏正確的軌道上。但請記住,可用的數字範圍不斷變化。如果用戶猜測90並且你的程序顯示「較低」,那麼89是新的最高可能數字;現在排除90到100。同樣,如果用戶猜測20並且程序說「更高」,那麼21是新的最低可能數字。您需要跟蹤最低和最高可能的數字,並且每個猜測都會更改其中一個數字。 – steveha

回答

0

每個猜測分區可用的數字範圍。

在第一次猜測之前,數字可以是1到100之間的任意值。假設用戶猜測「60」。這給了我們三種可能性:小於60,等於60或大於60.

現在您需要確定這三種可能性中的哪一種會使用戶猜測最長。我會給你一個提示:你不想說用戶是正確的,直到只有一個號碼留給用戶猜測,所以絕對不要接受「60」作爲第一個猜測。

您應該能夠編寫一個接受輸入的循環,然後根據輸入決定如何回答。例如,如果用戶的第一個猜測是「99」,那麼您肯定希望您的程序說「太大」。讓循環繼續運行,直到只剩下一個答案。

祝你好運,玩得開心!

+0

不,我的想法是選擇具有更多可能性的範圍,我只需要幫助將其轉換爲代碼!我覺得做這件事的唯一方法是單調乏味,重複性和永久性,而且我無法圍繞這個方向。我確定有更好的方法。 – user2892771

0

開始與一些變量來定義您的電流範圍即

int lower = 1; 
int upper = 100; 

創建這需要用戶的猜測的方法。這種方法應該根據猜測如何劃分範圍(即儘可能保持範圍儘可能大以達到目標)來確定哪個範圍界限要被替換。然後它應該返回(或打印)更高或更低,然後循環回到獲得用戶的另一個猜測

// For example 
Starts lower = 1, upper = 100 
User guesses 25 
Replace lower with 25 
say to guess higher 
Get another guess range is now lower = 25, upper = 100 
User guesses 75 
replace upper with 75 
say to guess lower 
get another guess range is now lower = 25, upper = 75 
etc, etc, etc, until guess has to be correct. 
+0

問題在於,我需要在任何時候允許任何猜測。即使它只能是25-75,該程序也需要能夠對97的猜測作出響應。 – user2892771

+0

那麼在這種情況下,您只是不會替換界限,因爲用戶顯然是延遲 –