1

代码尚未完成,但我遇到了一些问题。这是我当前的代码:

public class Engine
{
    public static void main(String[] args)
    {
        String word = JOptionPane.showInputDialog("Player 1! Please enter a word.");
        JOptionPane.showMessageDialog(null, "Switch to Player 2. It is time to play hangman!");
        char[] letters = word.toCharArray();
        String[] underscores = new String[word.length()];
        for(int i=0; i<=word.length()-1; i++)
        {
          underscores[i]="_ ";
        }
        StringBuilder builder = new StringBuilder();
        for (int i=0; i<word.length(); i++)
        {
            builder.append(underscores[i]);
        }
        int mistakes = 0;
        while(mistakes!=7)
        {
            String answer = JOptionPane.showInputDialog(null, "Please guess a letter.\n" + "Mistakes = " + mistakes + " of 7!\n\n" + builder.toString() + "\n\n");
            char guess = answer.charAt(0);
            for(int i=0; i<=word.length()-1; i++)
            {
                if(guess==letters[i])
                {
                    underscores[i] = answer;
                    for (int x=0; x<word.length(); x++)
                    {
                        builder.append(underscores[x]);
                    }
                }
            }
        }
    }
}

我遇到的问题是 StringBuilder 部分和 .append()

例如,如果这个词是“帽子”。将出现一个带有三个下划线的窗口,如下所示:


如果我猜“h”,那么它会变成:

_ _ _ H _ _

代替

H _ _

此外,如果我接下来猜测“t”,它将变为:

_ _ _ h _ _ h _ t

它将数组的更改元素添加到已经存在的数组中。我很确定它是由 append 方法引起的,但我不知道任何替代方法。

4

2 回答 2

0

尝试 的replace()delete()方法,或者简单地为每个猜测StringBuilder创建一个新的。StringBuilder

于 2013-11-05T04:45:07.677 回答
0

StringBuilder的append方法将新文本添加到现有文本中。这就是为什么你得到_ _ h _ _h_和东西。如果您使用 StringBuilder 是为了避免创建新的字符串对象,那么在附加文本之前,使用delete()方法清除构建器的现有文本,然后附加它。

builder.delete(0,builder.length()-1);
for(int x=0;x<words.length();x++) {
 builder.append(underscores[x]);
}

编辑:您的理解是正确的,可以在不使用 StringBuilder 的情况下构建这个游戏,也可以检查下面的代码。

import java.util.Scanner;

class Game {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    String word = null;
    char[] letters;
    char[] underScores;
    char guess;
    int found=0;
    int mistake=0;
    boolean present = false;

    System.out.println("Enter the word");
    word = s.nextLine();

    letters = word.toCharArray();
    underScores = new char[letters.length];
    for(int i = 0; i<letters.length;i++) {
      underScores[i]='_';
    }

    while(true) {
      System.out.println("The word is");
      for(int i = 0; i<letters.length;i++) {
        System.out.print(underScores[i]);
      }

      System.out.println("\nEnter a letter");
      guess= s.nextLine().charAt(0);

      for(int i =0; i<letters.length;i++) {
        if(guess==letters[i]) {
          underScores[i]=guess;
          found+=1;
          present=true;
          break;
        }
      }

      if(!present) {
        System.out.println("the letter is not present");
        mistake+=1;
      }

      if(mistake >=7) {
        System.out.println("too many mistakes!! sorry");
        break;
      }

      if(found==letters.length){
        System.out.println(word);
        System.out.println("you have found the word!!");
        break;
      }
    } //ending while
  } //ending main
} //ending Game
于 2013-11-05T04:48:04.757 回答