我目前正在做一个数组项目,在我的介绍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);
}
}
字符串在Java不变 – 2014-10-30 20:30:56
你有一个体面的开始,你需要使用一个循环来获得多个用户输入,直到他们赢或输... – brso05 2014-10-30 20:32:39
@Alex你可以使用一个字符数组而不是一个StringBuilder(我假设你没有覆盖StringBuilder)。 – brso05 2014-10-30 20:33:39