我正在尝试让Java将while循环的输出识别为变量,并在进一步的操作中使用该输出。
我想通过让一个玩家设置这个词而另一个玩家猜测它来尝试升级它。问题来自于使破折号的数量等于玩家输入的单词中的字母数,因此我将代码分开,这有效。
但是当我把它全部放回main
时,它将无法识别循环结束后有多少破折号;它只能识别只有1个破折号的初始值,因此它会产生问题。
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();
}
}
答案 0 :(得分:3)
以下代码似乎试图显示单词中可以找到正确选择的字母的位置
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
,那么输出应为
- I -
dash.substring 不重复短划线,它占用短划线的子部分,因为短划线是1个字母长,除了子串(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 {
然而,这留下了仅显示字母的第一个实例的问题。这可以使用另一个stack overflow answer来解决,我们在其中看到我们可以使用函数获得角色的所有出现
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;
}
然后使用该函数查找 all 字母出现的索引,我们可以处理重复的字母
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是一个类,通常写入
上骆驼案。答案 1 :(得分:2)
<强>解决方案强>
如果密码为pony
,则字符串dash
应等于----
。问题是,您实际上从未将dash
更改为等于-
。因此,当您执行dash.substring(SecretWord.indexOf(guess) + 1, dash.length())
之类的操作时,会出现错误,因为dash
只包含一个字符。这是我如何使dash
与秘密词的长度相同:
for(int i = 0; i < NumWord; i++) {
dash += "-";
}
在你的do-while循环之前直接插入一个更改,你的程序就像一个魅力。以下是为了进一步改进您的计划而需要考虑的其他一些事项。
提高可读性
NumWord
应为numWord
,SecretWord
应为secretWord
等。SecretWord.indexOf(guess) >= 0
应更改为
SecretWord.contains(guess)
游戏建议
和吊人一样,你应该展示所猜出的字母出现的所有地点。例如,如果密码字为happy
,则p
的猜测应生成--PP-
而不是--P--
的输出。
通常,即使不会导致错误,也不要接受错误输入。该计划不应允许以下任何情况:
!
)答案 2 :(得分:0)
我对您的代码进行了一些修改,现在似乎工作正常。 首先,我添加了一个额外的方法,只是为了让它更容易一些:
public static String printOutWord(String[] UpWord){
String out = "";
for(int i = 0; i < UpWord.length; i++){
out += UpWord[i];
}
return out;
}
以下是您编写的前几个更改:
String[] UpWord = new String[NumWord];
for(int i = 0; i < NumWord; i++){
UpWord[i] = "-";
}
printOutWord(UpWord);
System.out.println("\nWordGuess game!");
因此,您不再需要变量dash
,变量UpWord
已更改为字符串数组。
这是其余部分:
do {
System.out.println("Enter a letter (! to guess entire word): ");
guess = input.next().toUpperCase().trim();
Numguesses++;
if(guess.length() > 1){
System.out.println("Please only enter one letter at a time");
}else if (SecretWord.indexOf(guess) >= 0) {
int index = SecretWord.indexOf(guess);
UpWord[index] = guess;
while(SecretWord.indexOf(guess, index+1) >= index){
index = SecretWord.indexOf(guess, index+1);
System.out.println(index);
UpWord[index] = guess;
}
System.out.println(printOutWord(UpWord));
if(printOutWord(UpWord).equals(SecretWord)){
System.out.println("You won!\n" + "The secret word is " + SecretWord);
return;
}
} else {
if (guess.contains("!")) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase();
if (guess.equals(SecretWord)) {
System.out.println("You won!\n" + "The secret word is " + SecretWord);
System.out.println("You made " + Numguesses + " guesses");
} else if (!guess.equals(SecretWord)) {
System.out.println("You Lose!");
System.out.println("You made " + Numguesses + " guesses");
}
}
}
} while (!SecretWord.equals(guess));
input.close();
}
大多数更改都在第一个if语句中。 我希望这有帮助,如果需要澄清任何事情,请问,我很乐意提供帮助:)