2015-03-19 66 views
-2

好吧,所以我的猜測遊戲幾乎完成我只是不知道爲什麼中間部分不工作。基本上,用戶輸入一個五位數的數字,它應該在他們的猜測中加入數字,然後增加他們的總數,並告訴他們有多少數字他們得到正確,所以他們可以再次猜測,但它甚至沒有通過我的循環任何幫助,將不勝感激。Java猜測遊戲調試問題我無法解決

//Jonathan 
import javax.swing.JOptionPane; 

import java.util.Scanner; 
public class Guessing 
{ 

public static void main(String[] args) 
{ 
    //JOptionPane.showMessageDialog(null, "Welcome to Jonathan's number guessing game."); 
    int correctPosition = 0, sum = 0, numOfGuesses = 5, i; 
    String usersGuess, secretCode; 
    double number = Math.random() * 100000; 
    number = Math.round(number); 
    secretCode = Double.toString(number); 
    do 
    { 
    numOfGuesses--; 
    System.out.println(number); 
    Scanner keyboard = new Scanner(System.in); 
    usersGuess = keyboard.nextLine(); 
    if (usersGuess.equalsIgnoreCase(secretCode)) 
    { 
     JOptionPane.showMessageDialog(null, "You got the number correct!"); 
     System.exit(0); 
    } 
    else if (!(usersGuess.equalsIgnoreCase(secretCode))) 
    { 
    for (i=0; i > 5; i++) 
     { 
      int digit = Character.getNumericValue(usersGuess.charAt(i)); 
      if (digit == secretCode.charAt(i)) 
      System.out.println(digit); 
      { 
       correctPosition++; 
      } 

     } 
     JOptionPane.showMessageDialog(null, "You have " + correctPosition + " numbers in the correct position." + " The sum of which are\n" + sum + " keep guessing!"); 
    } 
    } while (numOfGuesses > 0); 

    } 
} 
+0

您的循環條件是不正確,應該是I'5.您應該通過增加一個調試器,或使用打印語句調試這些類型的問題你自己在周圍你感覺代碼沒有被執行。 – tinker 2015-03-19 00:05:30

+0

'if(digits == secretCode.charAt(i))'後面的行看起來不太正確 – MadProgrammer 2015-03-19 00:07:20

回答

3

幾件事情中脫穎而出......

首先...

for (i = 0; i > 5; i++) { 

i0i將小於5,這意味着i > 5false和環將終止...

我想你的意思是

for (i = 0; i < secretCode.length(); i++) { 

如果事實,因爲你需要在secretCodeuserGuess每個字符比較,你真的需要一個複合環

for (i = 0; i < secretCode.length(); i++) { 
    for (int x = 0; x < usersGuess.length(); x++) { 
     //... 
    } 
} 

接下來,你的usersGuess值轉換爲int,但是比較它的secretCodechar值...

int digit = Character.getNumericValue(usersGuess.charAt(i)); 
if (digit == secretCode.charAt(i)) 

這將永遠是平等的,相反,簡單地比較userGuesschar值對的secretCode

for (i = 0; i < secretCode.length(); i++) { 
    for (int x = 0; x < usersGuess.length(); x++) { 
     char digit = usersGuess.charAt(x); 
     if (digit == secretCode.charAt(i)) { 
      System.out.println(digit); 
      correctPosition++; 
     } 
    } 
} 

現在char值,這可能會產生假postives,更多的東西一樣......

for (i = 0; i < secretCode.length(); i++) { 
    if (i < usersGuess.length()) { 
     char digit = usersGuess.charAt(i); 
     if (digit == secretCode.charAt(i)) { 
      System.out.println(digit); 
      correctPosition++; 
     } 
    } 
} 

威力比較合適,但我也會添加一個if聲明來檢查usersGuesslength,以確保它等於的length並顯示錯誤如果用戶輸入無效值......這是我一直在做... 和...

if (digit == secretCode.charAt(i)) 
System.out.println(digit); 
{ 
    correctPosition++; 
} 

這是因爲實際執行[R消息...

if (digit == secretCode.charAt(i)) { 
    System.out.println(digit); 
} 
correctPosition++; 

哪個我不牛逼認爲這是你想要什麼,我想你想要的東西更像

if (digit == secretCode.charAt(i)) { 
    System.out.println(digit); 
    correctPosition++; 
} 
+0

非常感謝您的幫助。我的代碼工作 – 2015-03-19 00:40:33