Hangman Game帮助用字符串替换字符(java)

时间:2014-11-03 01:13:18

标签: java arrays string chars

我几乎知道如何做很多这个项目,除了用隐藏的单词替换隐藏单词的“_ _ _ ”,例如:“ o o _”。如何用字符串替换特征并输出数据。忽略笔记,它们只是一些随机的代码思想。以下是我到目前为止的情况:

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

  public class ProjectNum2 {

    // Alex Berencz

    public static void main(String[] args) {

        Scanner kbd = new Scanner(System.in);

        String letterguess;
        String wordguess;
        String hangmanwords = "";
        char letter;
        String con = "yes";
        String quit = "no";
        int computer = 0;
        int player = 0;
        boolean done = false;
        int guess = 6;

        // while(!done){

        System.out.println("Welcome to the Hangman Word game!");

        String[] hangmanWords = { "apple", "triangle", "assassin", "mercenary",
                "loop", "snake", "lion", "algorithm", "turtle", "return",
                "message", "heart", "halloween", "door", "fruit", "band",
                "married", "summer", "choice", "elephant" };
        String[] spaces = { "_ _ _ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _ _ _",
                "_ _ _ _ _ _ _ _ _", "_ _ _ _", "_ _ _ _ _", "_ _ _ _",
                "_ _ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _ _",
                "_ _ _ _ _ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _ _ _", "_ _ _ _",
                "_ _ _ _ _", "_ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _",
                "_ _ _ _ _ _", "_ _ _ _ _ _ _ _" };

        int index = (int) RandomWord(hangmanWords, spaces, hangmanwords);

        System.out.println();
        System.out.println("Choose a letter or enter zero to guess the word: ");
        letterguess = kbd.next();
        String guesscomplete = "0";
        String fixedkey = letterguess.toLowerCase();
        char[] charkey = new char[letterguess.length()];

        // for(int x=0; x>letterguess.charAt(x); x++){

        // charkey[x]=letterguess.charAt(x);

        if (letterguess.equals(guesscomplete)) {
            System.out.println("Guess the word! ");
            wordguess = kbd.next();

            if (wordguess.equals(hangmanWords[index])) {
                System.out.println("That is correct!");
                player++;

                System.out.println("Would you like to play again?");
                String again = kbd.next();

                if (again.equals(quit)) {
                    System.out.println("Computer wins: " + computer
                            + " Player wins: " + player);
                    System.out.println("See you later!");

                }

                else {
                    System.out.println("That is not correct!");
                    System.out.println("Would you like to play again?");
                    again = kbd.next();
                    computer++;

                }

            }

        }

        while (hangmanWords[index].contains(letterguess)) {
            System.out.println(hangmanWords[index].replace(hangmanWords[index],
                    letterguess));
            System.out
                    .println("Choose a letter or enter zero to guess the word: ");
            letterguess = kbd.next();

        }

        if (!hangmanWords[index].contains(letterguess)) {
            guess--;
            System.out.println("The letter is not in the word. You have "
                    + guess + " more guesses.");
            System.out
                    .println("Choose a letter or enter zero to guess the word: ");
            letterguess = kbd.next();

            if (guess == 0) {
                computer++;

            }

        }

    }

    // }

    // }

    private static Object RandomWord(String[] hangmanWords, String[] spaces,
            String hangmanwords) {

        Random ranIndex = new Random();
        int index = ranIndex.nextInt(hangmanWords.length);
        hangmanwords = spaces[index];
        System.out.print(hangmanwords);
        return (index);

    }

  }

这是我的输出:

 Welcome to the Hangman Word game!
_ _ _ _ _ _ _
Choose a letter or enter zero to guess the word: 
 a
a
Choose a letter or enter zero to guess the word: 

2 个答案:

答案 0 :(得分:0)

StringBuilder有一个replace方法,它接受一个开始和结束索引 String上的replace方法只接受现有的字符替换。

答案 1 :(得分:0)

你不需要那样替换。

您只需要根据“猜到的字符”想出一种显示答案的方法。

例如,

for each char c in answer {
    if exists in guessedChars {
        print c
    } else {
        print _
    }
}