这是我在这个网站上的第一个问题,所以我会尽量不成为一个完全的菜鸟..
我目前正在用java创建刽子手游戏。所以我的问题是如果给我们一个单词“ghost”并且ghost被替换为“_”,
hiddenWord = ghost.length();
for (i=0; i < ghost.lenth(); i ++)
System.out.print("_ ")
给我们一个输出
“_ _ _ _ _”
假设我们猜测字母“O”,字母“o”被猜测,我该如何替换`
"_ _ _ _ _" with
"_ _ o _ _ "
我当前的类文件
public void pickWord()
{
String[] listOfWords;
listOfWords = new String[10];
listOfWords[0] = "shenanigans";
listOfWords[1] = "conversely";
listOfWords[2] = "octopus";
listOfWords[3] = "dizzy";
listOfWords[4] = "malicious";
listOfWords[5] = "goosebumps";
listOfWords[6] = "flying";
listOfWords[7] = "staff";
listOfWords[8] = "xylophone";
listOfWords[9] = "zapping";
Random generator = new Random();
int lineNumber = generator.nextInt(9);
disguisedWord = listOfWords[lineNumber];
}
public void displayMark()
{
for( int i = 0; i < disguisedWord.length(); i ++)
underscore = underscore + "_ ";
System.out.println(underscore);
}
public void makeGuess() throws IOException
{
System.out.println("Your word is " + disguisedWord.length() + " letters long.");
System.out.println("Feel free to guess a letter.");
guess = (char)System.in.read();