无阵列的java刽子手游戏
这段代码的目标是创建一个基本的刽子手游戏。直到最后一步,一切正常。在用户猜出所有字母后,它应该显示无阵列的java刽子手游戏,java,Java,这段代码的目标是创建一个基本的刽子手游戏。直到最后一步,一切正常。在用户猜出所有字母后,它应该显示“您猜到了”,但循环似乎额外执行了一次。我尝试将循环代码更改为: while(猜字母”; userChoice=Character.toUpperCase(input.nextLine().charAt(0)); } System.out.println(猜字母); } 在while循环中获取用户输入。在最后一个循环中,您验证了用户输入,然后在循环结束时请求另一个用户输入。游戏尝试并完成,但在完成之
“您猜到了”
,但循环似乎额外执行了一次。我尝试将循环代码更改为:while(猜字母
但这只会让循环过早结束一次。非常感谢您的帮助
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;
}
}
问题是,您总是在递增
猜测字母后再次询问。所以它总是会多问一次。尝试重新排序,例如:
//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);
}
//System.out.println(“请猜一个字母”);
//userChoice=Character.toUpperCase(input.nextLine().charAt(0))//获取输入并转换为大写以进行比较
final int asteriskAmount=hint.length()-1;//“1”表示空间。
int guessedLetters=2;//对于已显示的“G”和“T”。
while(猜字母>”;
userChoice=Character.toUpperCase(input.nextLine().charAt(0));
}
System.out.println(猜字母);
}
在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));
}
System.out.println(“提示为”+hint);
//在循环内移动下面的两行
//System.out.println(“请猜一个字母”);
//userChoice=Character.toUpperCase(input.nextLine().charAt(0))//获取输入并转换为大写以进行比较
int asteriskAmount=hint.length()-1;//“1”表示空间。
int guessedLetters=2;//对于已显示的“G”和“T”。
while(猜字母
从循环之前删除这些行
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));
}
}
嘿,谢谢你的回答。如果没有数组的情况下出现了不止一次,我怎么用字母替换星号呢?我在想,与其使用checkLetter
方法返回布尔值,不如使用int
值来告诉我该字母出现的时间,以及然后做一些循环,用字母替换星号。还有其他建议吗?很抱歉,我没有早点回复,我正在路上。我已经更新了我的答案,在不使用数组的情况下实现了相同的结果。看一看,看看你的想法:)非常感谢你的帮助。我只是想指出,你需要添加中的de>()
s
啊,我很困惑,因为数组也有.length
但没有()
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++;
}
}
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));
}
}