2014-10-30 39 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));         
    }                   
}