简体   繁体   English

当有正确的用户输入时(Hangman Game)如何跳出循环?

[英]How to break out of a loop when there is correct user input (Hangman Game)?

I can't figure out how to terminate the loop whenever my user guessed the word correctly. 每当我的用户正确猜出单词时,我都无法弄清楚如何终止循环。

I can either do something like this or use a substring function to take out the letters of the word one by one. 我可以做这样的事情,也可以使用子字符串函数来逐个取出单词的字母。

Here is what I have so far: 这是我到目前为止的内容:

package mcneil;

import java.util.Scanner;
import java.util.Random;

public class HangmanGame
{

public static void main(String[] args)
{
    Scanner ui = new Scanner(System.in);
    Random rnd = new Random();      
    String words[] = {"Mac", "Windows", "DOS", "Linux", "Solaris"};
    String man[] ={"  ______\n"
                 + "  |    |\n"
                 + "  |\n"
                 + "  |\n"
                 + "  |\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |\n"
                 + "  |\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |    |\n"
                 + "  |\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |    |/\n"
                 + "  |\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |   \\|/\n"
                 + "  |\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |   \\|/\n"
                 + "  |   /\n"
                 + "_____",

                   "  ______\n"
                 + "  |    |\n"
                 + "  |    O\n"
                 + "  |   \\|/\n"
                 + "  |   / \\\n"
                 + "_____"};
    String guess;
    int guesses = 0;
    String guessedLetters = " ";

    boolean wordGuessed = false;

    System.out.println(man[0]);
    String answer = words[rnd.nextInt(words.length)];

    System.out.println("I'm thinking of a word that contains "
                        + answer.length() +  " letters\n");

    do
   {           
       for(char letter : answer.toCharArray())
       {
           if(guessedLetters.indexOf(letter)== -1)
           {
               System.out.print('*');
           }
           else
           {

               System.out.print(letter);
           }

       }
       System.out.println("\nPlease enter a letter: ");
       guess = ui.next();

       guessedLetters += guess + " ";

       System.out.println("You have guessed the letters: " 
               + guessedLetters);

       // not a good guess
       if(!answer.contains(guess))
       {
           System.out.println(man[guesses + 1]);
           guesses++;
           //some kind of counter
       }
       else {
       }if(answer.length() == 0)
       {
            wordGuessed = true;
       }
   }while(!wordGuessed && guesses < 6);            
    }    
}

You have conditions indicating whether a guess is contained in the word, and if a letter in the answer is in the guessed letters. 您有条件指出单词中是否包含猜测,以及答案中是否包含字母。 You need something tracking whether the answer is complete. 您需要一些跟踪答案是否完整的信息。 You could count the non-* letters it the guessed answer, perhaps. 也许您可以算出非*字母作为猜测的答案。 Or there are other ways. 或者还有其他方法。

Do something like: 做类似的事情:
Make words a hashset. 使单词成为哈希集。

 while(GuessList.hasNext())
  {
 if(!(words.remove(GuessList.next()))
 {
      System.out.println(" The next part of hangman");

 }


 }

if(words.size==0)
  {
   System.out.println("You have guessed it right");
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM