我向Java大师提出了另一个nooby问题
基本上我想要的是:
_ _ _ _;但是,当我试图隐藏单词的下划线时,我可以进入第二步,它显示为____而不是_ _ _ _。
它的代码:
//Randomly picks word from Array which played
public String pickWord(){
String guessWord = (wordsList[new Random().nextInt(wordsList.length)]);
return guessWord;
}
//Hides picked word
public char[] setWord(){
word = pickWord().toCharArray();
for (int i = 0; i < Array.getLength (word); i++) {
word[i] = '_';
}
return word;
}发布于 2012-08-11 15:17:15
您需要允许新char数组中的空格:
String testWord = "test";
char[] word = new char[testWord.length() * 2];
for (int i = 0; i < word.length; i+=2) {
word[i] = '_';
word[i + 1] = ' ';
}发布于 2012-08-11 15:18:09
在for-循环中添加额外条件
if(i%2 == 0)
word[i]='_';
else
word[i]=' ';或者仅当i%2 =0时才重写wordi。只有一个如果在你有
word[i]='_';
if(i%2 != 0)
word[i]=' '; 发布于 2012-08-11 15:18:28
当您将'‘存储在word变量中并打印出来时,它会显示"__“,因为它显示了连续的'’。你可以用不同的方式来做,就像你可以在单词的每一个字符上放置一个空格,或者在word变量中填充一个空格。
for (int i = 0; i < Array.getLength (word); i++) {
word[2*i] = '_';
word[2*i+1] = ' ';
}https://stackoverflow.com/questions/11915803
复制相似问题