我正在尝试让Java将while循环的输出识别为变量,并在进一步的操作中使用该输出。
我想尝试让一位玩家设置单词,而另一位则猜猜它来升级它。问题出在使破折号等于玩家输入的单词中的字母数,所以我把代码分离出来了,这行得通。
但是,当我把它们全部放回去时main
,它不会识别出循环结束后有多少破折号。它只能识别只有一个破折号的初始字符,因此会带来问题。
编辑:非常感谢你们,这是我第一次在堆栈溢出,再次是tnx。像魅力一样工作:D
package iB;
import java.util.Scanner;
import java.lang.String;
public class WordGuess {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String secretWord ;
String guess, dash = "-", upWord;
int numGuesses = 0;
int numWord;
final String SENTINEL = "!";
System.out.println("Player 2, please look away. Player 1, please enter the secter word: \n");
secretWord = input.next().toUpperCase().trim();
numWord = secretWord.length();
//System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(int dashNum = 1; dashNum < numWord; dashNum++) {
dash += "-" ;
}
System.out.println("WordGuess game!\n");
do {
System.out.println("Enter a letter (" + SENTINEL + "to guess entire word): ");
guess = input.next().toUpperCase().trim();
numGuesses ++;
if (secretWord.contains(guess) && guess.length() == 1) {
upWord = dash.substring(0, secretWord.indexOf(guess));
upWord += guess;
upWord += dash.substring(secretWord.indexOf(guess) + 1, dash.length());
dash = upWord.toUpperCase();
System.out.println(dash);
if (dash.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses."); }
} else if (guess.length() >= 2) {
System.out.println("Please only enter one letter at a time! \n"); }
if (guess.contains(SENTINEL)) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase().trim();
if (guess.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
} else {
System.out.println("You Lose!");
System.out.println("The secret word was " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
}
}
} while(!guess.contains(SENTINEL));
input.close();
}
}
以下代码段似乎试图显示单词中正确选择的字母的位置
if (SecretWord.indexOf(guess) >= 0) {
UpWord = dash.substring(0, SecretWord.indexOf(guess));
UpWord += guess;
UpWord += dash.substring(SecretWord.indexOf(guess) + 1, dash.length());
System.out.println(UpWord);
} else {
因此,如果单词是this
并且您猜到了,i
那么输出应该是
- 一世-
dash.substring不重复破折号,它是破折号的一个子部分,因为破折号的长度为1个字母,除substring(0,1)以外的任何内容都将导致异常。
我相信您想重复dash
直到找到猜到的字母,然后再重复直到单词的结尾。类似于以下内容:
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
String outString="";
for(int i=0;i<guessedIndex;i++){
outString+=dash; //repeat dash until we get to the correctly guessed letter
}
outString+=guess; //put the letter in
for(int i=guessedIndex;i<SecretWord.length();i++){
outString+=dash; //repeat dash until we get to end of the word
}
System.out.println(outString);
} else {
但是,这留下了仅显示字母的第一个实例的问题。可以使用另一个堆栈溢出答案来解决此问题,在该答案中,我们看到可以使用函数来获取字符的所有出现次数
public static ArrayList<Integer> getAllIndexes(String testChar, String string){
int index=string.indexOf(testChar);
ArrayList<Integer> indexes=new ArrayList<Integer>();
while(index>0){
indexes.add(index);
index=string.indexOf(testChar,index+1);
}
return indexes;
}
然后使用该函数查找出现字母的所有索引,我们可以处理重复的字母
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
ArrayList<Integer> indexes=getAllIndexes(guess,SecretWord);
String outString="";
for(int i=0;i<SecretWord.length();i++){
if (indexes.contains(i)){
outString+=guess; //if its one of the guessed letters, put that in
}else{
outString+=dash; //otherwise add a dash
}
}
System.out.println(outString);
} else {
现在一个单词hello
和一个l
正确输出的猜测--LL-
SecretWord
应该这样secretWord
。就目前而言,它看起来像SecretWord是一个类,通常以大写字母大写。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句