У меня небольшая проблема при попытке сделать игру в палач. Раньше я писал о другой ошибке, но теперь я сталкиваюсь с новой, которую я не могу понять. Я пытаюсь проверить, что письмо не было введено. Но он пропускает весь раздел инструкции if/else. Когда я запускаю этот код:Java - Создание игры для палача
общественного класса TestingStuff {
static StringBuffer randomWord;
static Scanner console = new Scanner(System.in);
static int totalTries = 1;
static String guess;
static char finalGuess;
public static void main(String[] args) throws Exception {
randomWord = TestingStuff.sendGet();
char[] guesses = new char[26];
int length = randomWord.length();
System.out.print("* * * * * * * * * * * * * * *"
+ "\n* Welcome to Hangman! *"
+ "\n* * * * * * * * * * * * * * *");
System.out.println("\nYou get 10 tries to guess the word by entering in letters!\n");
System.out.println(randomWord);
/*
Cycles through the array based on tries to find letter
*/
while (totalTries <= 10) {
System.out.print("Try #" + totalTries + "\nWord: " + makeDashes(randomWord));
//Right here: Search through the array of guesses, make it 26 characters to represent the alphabet
//if the user guess equals an already guessed letter, add to try counter. If it's correct, then reveal the letter that is
//correct and do it again without adding to the try counter.
System.out.print("\nWhat is your guess? ");
guess = console.nextLine();
finalGuess = guess.charAt(0);
guesses[totalTries - 1] = finalGuess; //Puts finalGuess into the array
for (int i = 0; i < totalTries; i++) { //checks to see if the letter is already guessed
if (guesses[i] != finalGuess) {
System.out.println(guesses[i]);
for (int j = 0; i < length; j++) { //scans each letter of random word
if (finalGuess == randomWord.charAt(j)) {
//put a method that swaps out dashes with the guessed letter
totalTries++;
}
}
} else {
System.out.println("Letter already guessed, try again! ");
}
}
}
}
Я получаю выход этого:
* * * * * * * * * * * * * * *
* Welcome to Hangman! *
* * * * * * * * * * * * * * *
You get 10 tries to guess the word by entering in letters!
ostracization
Try #1
Word: -------------
What is your guess? a
Letter already guessed, try again!
Try #1
Word: -------------
What is your guess?
Это просто сказать, что письмо уже догадались, когда есть пустой элемент в массиве. Я что-то упустил?
'if (guesses [i]! = FinalGuess) {} else {« Письмо уже догадалось, попробуйте еще раз! »}' Довольно ясно. Если вы хотите управлять элементом «пустой» (что является пустым?), Тогда вам нужно добавить условие для него. – m0skit0
@ m0skit0, когда я предлагаю пользователю ввести в предположение, у меня есть он, поэтому он поместит его в массив значений char. Разве это не то, что я здесь делаю? – Shayd3