2014-10-28 87 views
2

現在好了,在課堂上,我們被分配另一個劊子手項目與數組的使用,我們不得不產生「_」的。 當用戶輸入了「wordAnswer」我已經成功地生成所有的空白。 我的問題是,我無法弄清楚如何與空白代替正確的猜測字符「_」。劊子手第2部分使用數組的Java

這裏是我的整個程序代碼,我知道它不是全部齊全。

import cs1.Keyboard; 
public class Game { 

    public static void main(String[] args) { 
     int correctGuess = 0; 
     int wrongGuess = 0; 
     int bodyParts = 0; 
     char guess; 
     boolean foundIt; 
     System.out.println("Welcome to Hangman!"); 
     System.out.println("Please enter a word for the other user to guess!"); 
     String wordAnswer = Keyboard.readString(); 
     char[] chars = wordAnswer.toCharArray(); 
     char[] blanks = new char[wordAnswer.length()]; 

     System.out.println("You have 6 attempts to guess the word!"); 
     do { 
      for (int i = 0; i < wordAnswer.length(); i++) { 
       System.out.print(" _ "); 
      } 
      System.out.println("Please enter in your guess!"); 
      guess = Keyboard.readChar(); 
      for (int i = 0; i < chars.length; i++) { 
       if (guess == chars[i]) { 
        blanks[i] = guess; 
        foundIt = true; 
       } 
      } 
     } while (correctGuess < wordAnswer.length() && wrongGuess != 6); { 
      System.out.println("Congratulations! You have solved the word!"); 
     } 
    } 
} 

我知道,你必須使用一個for循環才能夠空白更改爲正確的猜測字母,但是當你運行該程序,並在正確的猜測進入空白不會變成正確的信。 這是我的OutPut

Welcome to Hangman! 
Please enter a word for the other user to guess! 
program 
You have 6 attempts to guess the word! 
_ _ _ _ _ _ _ Please enter in your guess! 
p 
_ _ _ _ _ _ _ Please enter in your guess! 
r 
_ _ _ _ _ _ _ Please enter in your guess! 
o 
_ _ _ _ _ _ _ Please enter in your guess! 
g 
_ _ _ _ _ _ _ Please enter in your guess! 
r 
_ _ _ _ _ _ _ Please enter in your guess! 
a 
_ _ _ _ _ _ _ Please enter in your guess! 
m 
_ _ _ _ _ _ _ Please enter in your guess! 

我期待的是!

Welcome to Hangman! 
Please enter a word for the other user to guess! 
program 
You have 6 attempts to guess the word! 
_ _ _ _ _ _ _ Please enter in your guess! 
p 
p _ _ _ _ _ _ Please enter in your guess! 
r 
p r _ _ _ _ _ Please enter in your guess! 
o 
p r o _ _ _ _ Please enter in your guess! 

等等

這是我,但提示和幫助將是非常感激!

System.out.println("Please enter in your guess!"); 
     guess = Keyboard.readChar(); 
     for (int i = 0; i < chars.length; i++) { 
      if (guess == chars[i]) { 
       blanks[i] = guess; 
       foundIt = true; 
      } 
     } 
+3

「不工作」 是不是一個有用的問題說明。 *它是如何「不工作」?編譯錯誤?運行時異常?什麼是錯誤/異常?您是否通過Google努力瞭解它是什麼以及它發生的原因?或者這是不正確的行爲?發生了什麼?你期望會發生什麼?更詳細。 – tnw 2014-10-28 19:56:49

+0

還不是很清楚。你期望看到第二行的'_ _ _ _ _ _ _ _',第三行的'p _ _ _ _ _'等嗎? – Andrew 2014-10-28 20:03:47

+0

好吧,我已經更新了它,我不認爲我真的可以更具體。我正在尋找的是正確的猜測字母來代替「_」空白。 – 2014-10-28 20:03:49

回答

0

行後

char[] blanks = new char[wordAnswer.length()]; 

添加以下內容:

for(int i = 0; i < wordAnswer.length(); i++) { 
    blanks[i] = '_'; 
} 

然後,而不是System.out.print(" _ "),做System.out.print(" " + blanks[i] + " ");

只要你知道,你需要解決什麼在do ... while循環之後。它會錯誤地告訴該人他們已經成功地解決了這個詞,即使他們沒有成功。此後你也不需要{ ... }。此外,您foundIt變量不做任何事情,但你會需要它來處理不正確的猜測,一旦你到這一點..

0

你非常接近。只是幾個調整。

右後

char[] blanks = new char[wordAnswer.length()]; 

添加

Arrays.fill(blanks, "_"); 

,或者,如果你不能使用Arrays類,然後做舊的方式:

for (int i = 0; i < blanks.length; i++) { 
    blanks[i] = " _ "; 
} 

而不是

for (int i = 0; i < wordAnswer.length(); i++) { 
    System.out.print(" _ "); 
} 

+0

好的,謝謝你的提示,但如何用正確的猜測字母替換「_」空格呢? – 2014-10-28 20:36:23

+0

你已經在做:'blanks [i] = guess;' – 2014-10-28 20:40:34