2014-10-30 105 views
0

我目前正在做一個數組項目,在我的介紹CS類,我必須成功地做一個hang子手遊戲。我認爲我開始的時候相當合適,但我似乎無法掌握如何將字符替換爲字符串。我必須有一種方法來創建一個隨機單詞,所以這就是爲什麼我在我的代碼中有一個方法。看看我到目前爲止:如何獲得一個字符替換字符串中的部分(java)

import java.util.Random; 
import java.util.Scanner; 
public class ProjectNum2 { 
//creator's name 

    public static void main(String[] args) { 

     System.out.println("Welcome to the Hangman Word game!"); 

     String[] wordKey = { 
          "loop", 
          "for", 
          "while", 
          "java", 
          "switch", 
          "scanner", 
          "else", 
          "double", 
          "integer", 
          "public", 
          "static", 
          "method", 
          "return", 
          "null", 
          "void", 
          "true", 
          "false", 
          "import", 
          "string", 
          "character" 
          }; 
     String[] wordSpace = { 
           "_ _ _ _", 
           "_ _ _", 
           "_ _ _ _ _", 
           "_ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _ _", 
           "_ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _", 
           "r _ _ _ _ _", 
           "_ _ _ _", 
           "_ _ _ _", 
           "_ _ _ _", 
           "_ _ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _", 
           "_ _ _ _ _ _ _ _ _" 
          }; 
     char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 

     final int guesses = 6; 

     int index = 0; 
     index=(int) randomGen(wordKey, wordSpace); 

     Scanner keyboard = new Scanner(System.in); 
     System.out.println(); 
     System.out.print("Choose a letter or enter zero to guess the word: "); 
     char letter = keyboard.nextLine().charAt(0); 
    } 

    private static Object randomGen(String[] wordKey, String[] wordSpace) { 
     String gameWord; 
     Random randIndex = new Random(); 
     int index = randIndex.nextInt(wordKey.length); 
     gameWord = wordSpace[index]; 
     System.out.print(gameWord); 
     return (index); 

    } 

} 
+0

字符串在Java不變 – 2014-10-30 20:30:56

+0

你有一個體面的開始,你需要使用一個循環來獲得多個用戶輸入,直到他們贏或輸... – brso05 2014-10-30 20:32:39

+1

@Alex你可以使用一個字符數組而不是一個StringBuilder(我假設你沒有覆蓋StringBuilder)。 – brso05 2014-10-30 20:33:39

回答

0

字符串是不可變的,這意味着一旦你創建它們,你不能改變它們。改爲使用StringBuilder類。下面是一個簡單的例子,讓你開始使用

StringBuilder sb = new StringBuilder(30); 
    sb.append("Hello Matthew"); 

    // append a character 
    char c = '!'; 
    sb.append(c); 
1

Java中的字符串是不可改變的,這意味着你不能改變它。你必須建立一個新的String對象,在你的情況下使用String.replace(...)方法。

+0

如果他採用了完全不同的方法,這可能會奏效。忘記「空格」數組(這將是很好的,因爲它高度耦合到'wordKey'數組)。除非已經猜到字母,否則嘗試用''_''替換''''''''中的每個字母。當什麼都沒有被正確猜測,你會得到'詞彙空間'會給你什麼。當這個過程產生一個沒有「__」的單詞時,他們就贏了。 – CandiedOrange 2014-10-30 21:08:14

0

爲了讓你在正確的軌道上,而不是破壞所有的樂趣,這裏是從字符串"loop"和焦炭'o'導出並打印字符串_oo_的方法:

class Test {                 
    public static void main(String[] args) {         
    String word="loop";   

    char[] array = new char[word.length()];         
    for(int i=0; i<array.length; i++) {          
     array[i] = '_';               
    }                   

    char guess = 'o';               
    for(int i=0; i<word.length(); i++) {          
     if(word.charAt(i) == guess) {           
     array[i] = guess;              
     }                  
    }                   

    System.out.println(new String(array));         
    }                   
}   
相關問題