此代码的目标是创建一个基本的刽子手游戏。一切正常,直到最后一步。在用户猜出它应该显示"You guessed it"
的所有字母后,但循环似乎执行了一个额外的时间。我尝试将循环代码更改为:
while(guessedLetters < asteriskAmount - 1)
但这只是过早地使循环结束了一次
非常感谢任何帮助。
import java.util.Scanner;
public class SecretPhrase {
public static void main(String[] args) {
char userChoice;
String secretPhrase = "GO TEAM";
Scanner input = new Scanner(System.in);
String hint = "G* T***";
StringBuilder secretWord = new StringBuilder(hint);
System.out.println("The hint is " + hint);
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0)); //Taking the input and turning to uppercase for comparing purposes
int asteriskAmount = hint.length() - 1; // "-1" for the space.
int guessedLetters = 2; // for the 'G' and the 'T' that are already displayed.
while(guessedLetters < asteriskAmount) {
boolean isInPhrase = checkLetter(userChoice, secretPhrase);
if(isInPhrase) { // if the guessed letter is a letter in the phrase...
int position = getPosition(userChoice, secretPhrase);
secretWord.setCharAt(position, userChoice);
hint = secretWord.toString();
guessedLetters++;
System.out.println(secretWord);
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
else {
System.out.println("That letter is not in the phrase. Please try again >>> ");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
}
System.out.println("You got it! The secret word is " + secretWord);
}
public static boolean checkLetter(char userChoice, String secretPhrase) {
boolean isInPhrase = false;
int amountOfLetters = 0;
for(int x = 0; x < secretPhrase.length(); x++) {
if(userChoice != secretPhrase.charAt(x)) {
isInPhrase = false;
}
else {
isInPhrase = true;
amountOfLetters++;
}
}
if(amountOfLetters >= 1) {
isInPhrase = true;
}
return isInPhrase;
}
public static int getPosition(char userChoice, String secretPhrase) {
int position;
int x = 0;
while(userChoice != secretPhrase.charAt(x)) {
x++;
}
position = x;
return position;
}
}
这是我的输出:
The hint is G* T***
Please guess a letter
**o**
GO T***
Please guess a letter
**e**
GO TE**
Please guess a letter
**a**
GO TEA*
Please guess a letter
**m**
GO TEAM
Please guess a letter
**a**
You got it! The secret word is GO TEAM
答案 0 :(得分:0)
问题是你在递增guessedLetters
后总是再问。所以它总是要求一个额外的时间。尝试重新排序,例如:
//System.out.println("Please guess a letter");
//userChoice = Character.toUpperCase(input.nextLine().charAt(0)); //Taking the input and turning to uppercase for comparing purposes
final int asteriskAmount = hint.length() - 1; // "-1" for the space.
int guessedLetters = 2; // for the 'G' and the 'T' that are already displayed.
while (guessedLetters < asteriskAmount) {
System.out.println(secretWord);
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
final boolean isInPhrase = checkLetter(userChoice, secretPhrase);
if (isInPhrase) { // if the guessed letter is a letter in the phrase...
final int position = getPosition(userChoice, secretPhrase);
secretWord.setCharAt(position, userChoice);
hint = secretWord.toString();
guessedLetters++;
} else {
System.out.println("That letter is not in the phrase. Please try again >>> ");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
System.out.println(guessedLetters);
}
答案 1 :(得分:0)
在while循环中抓取用户输入。发生的事情是,在最后一个循环中,您将验证用户输入,然后在循环结束时请求另一个用户输入。游戏尝试并完成,但在它完成之前,它必须通过while
中的最后一个用户输入。我总是尝试将我的用户输入放在循环的开头,所以这样的事情不会发生。希望这可以帮助!
System.out.println("The hint is " + hint);
//Move the 2 lines below inside your loop
//System.out.println("Please guess a letter");
//userChoice = Character.toUpperCase(input.nextLine().charAt(0)); //Taking the input and turning to uppercase for comparing purposes
int asteriskAmount = hint.length() - 1; // "-1" for the space.
int guessedLetters = 2; // for the 'G' and the 'T' that are already displayed.
while(guessedLetters < asteriskAmount) {
//Moved from above
System.out.println(secretWord);
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
boolean isInPhrase = checkLetter(userChoice, secretPhrase);
if(isInPhrase) { // if the guessed letter is a letter in the phrase...
int position = getPosition(userChoice, secretPhrase);
secretWord.setCharAt(position, userChoice);
hint = secretWord.toString();
guessedLetters++;
//These are not needed any more
//System.out.println(secretWord);
//System.out.println("Please guess a letter");
//userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
答案 2 :(得分:0)
从循环之前删除这些行
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
现在在循环内部,将相同的行移动到循环的开头。
while(guessedLetters < asteriskAmount) {
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
boolean isInPhrase = checkLetter(userChoice, secretPhrase);
if(isInPhrase) { // if the guessed letter is a letter in the phrase...
int position = getPosition(userChoice, secretPhrase);
secretWord.setCharAt(position, userChoice);
hint = secretWord.toString();
guessedLetters++;
System.out.println(secretWord);
}
else {
System.out.println("That letter is not in the phrase. Please try again >>> ");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
}
编辑:要在不使用数组的情况下检查所选字母的多个实例,请尝试使用此代替替换代码
for(int r=0; r<secretPhrase.length(); r++){
if(secretPhrase.charAt(r) == userChoice){
secretWord.setCharAt(r, userChoice);
guessedLetters++;
}
}
有了这个,您不再需要getPosition
方法或position
变量。
建议更改的完整循环
while(guessedLetters < asteriskAmount) {
System.out.println("Please guess a letter");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
boolean isInPhrase = checkLetter(userChoice, secretPhrase);
if(isInPhrase) { // if the guessed letter is a letter in the phrase...
for(int r=0; r<secretPhrase.length(); r++){
if(secretPhrase.charAt(r) == userChoice){
secretWord.setCharAt(r, userChoice);
guessedLetters++;
}
}
System.out.println(secretWord);
}
else {
System.out.println("That letter is not in the phrase. Please try again >>> ");
userChoice = Character.toUpperCase(input.nextLine().charAt(0));
}
}