被我的 Java 作业困扰 - 使用 StringBuilder 进行绞刑吏游戏 - 有帮助吗?

发布于 2024-10-02 22:09:12 字数 346 浏览 6 评论 0原文

*注意:我不是要求你做我的作业。我只是被困住了。

我现在正在设计一个 Hangman 类。显然,我们需要三个 StringBuilder (a) 一个用于显示连字符:"--------" 单词的长度,b) 一个用于显示猜测的正确字母:" --a--e---",最后 c) 另一种本质上与 b 相反(猜测的字母被连字符替换,未猜测的字母被显示)。 c)的目的是看猜测时是否有匹配。

我最大的问题是我在 Google 上找不到很多实用的 StringBuilder 示例,即我最大的问题是我可以/应该在哪里实例化 Hangman 类中的三个 StringBuilder?

谢谢...

*NOTE: I AM NOT ASKING YOU TO DO MY HOMEWORK. I am just stuck.

I am designing a Hangman class right now. Apparently, we need three StringBuilders (a) one to display hyphens: "--------" the length of the word, b) one to display correct letters guessed: "--a--e---", and finally c) another one that is essentially the opposite of b (guessed letters replaced by hyphens and unguessed letters revealed). The purpose of c) is to see if there are any matches during guessing.

My biggest problem is I can't find many practical StringBuilder examples on Google, namely my biggest issue is where can/should I instantiate the three StringBuilders in the Hangman class?

thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-10-09 22:09:12

我在这里猜测您有一个 Hangman 类,它作为一个模型执行三件事(与此相关),即:

  • 为您提供一个带有 1 的字符串 - 对于要猜测的单词中的每个字符
  • 为您提供一个显示在正确的位置正确猜测的字符
  • 为您提供一个字符串,显示已使用的字符

这些都取决于模型的状态,这将是

  • 单词字符
  • 猜测的

基于此,我想说您应该有三种返回的方法字符串,并在每个方法中创建一个新的 StringBuilder 实例。构建字符串与状态是分开的,只是为了明确为什么我不同意 Computerish。

StringBuilder 是一种比仅使用连接更有效的构建字符串的方法,但它很容易使用。您首先创建它的一个实例。

StringBuilder builder = new StringBuilder();

然后你通过附加字符串或字符(或其他东西)来构建字符串:

builder.append('-');
builder.append('w');

完成后,你从 StringBuilder 构建一个 String 实例:

String string = builder.toString();

最后得到“-w”,这是一个相当无聊的例子。

I'm guessing here that you have a Hangman class which works as a model that does three things (relevant for this) which is:

  • Gives you a string with one - for each character in the word to guess
  • Gives you a string which shows the correctly guessed characters in the right position
  • Gives you a string which shows which characters have been used

These are all dependent on the state of the model which would be

  • the word
  • characters guessed

Based on that I'd say that you should have three methods that return Strings and in each of those methods you create a new StringBuilder instance. Building a string is separate from the state just to make it clear why I disagree with Computerish.

StringBuilder is a more efficient way to build up strings then just using concatenation, but it is easy to use. You start with creating an instance of it.

StringBuilder builder = new StringBuilder();

Then you build up the String by appending Strings or chars (or other things):

builder.append('-');
builder.append('w');

When you are done you construct a String instance from the StringBuilder:

String string = builder.toString();

and you end up with "-w" which is a rather boring example.

祁梦 2024-10-09 22:09:12

一个简短的例子:

StringBuilder helloWorldBuilder = new StringBuilder();
helloWorldBuilder.append("Hello");
helloWorldBuilder.append(" ");
helloWorldBuilder.append("World");
helloWorldBuilder.append("!");
String helloWorld = helloWorldBuilder.toString();

是的,您可以创建任意数量的 StringBuilder 对象(假设您有无限的堆,但实际上是这样的:“您喜欢多少就多少”,它比 3 更大:- ) )

A short example:

StringBuilder helloWorldBuilder = new StringBuilder();
helloWorldBuilder.append("Hello");
helloWorldBuilder.append(" ");
helloWorldBuilder.append("World");
helloWorldBuilder.append("!");
String helloWorld = helloWorldBuilder.toString();

And yes, you can create as many StringBuilder objects as you like (assuming you have an unlimited heap, but practically spoken: "as many as you like" which is bigger then 3 :-) )

折戟 2024-10-09 22:09:12

StringBuilder 的目的是在使用字符串之前构建字符串。每次都应该重新创建它们(这是解决即将出现的更棘手问题的良好实践)。因此,不要考虑 stringbuilder 类,而是考虑如何构建您需要的字符串。假设您将正确的猜测存储在 char 数组中,则可以使用三种方法根据需要构建字符串。在其中一种情况下,您根本不需要 StringBuilder。

您的 ToBlankFormat 方法可能如下所示:

public String toBlankFormat()
{
    char[] format = new char[answer.length()];
    Arrays.fill(format, '-');
    return new String(format);
}

本质上,要使用 StringBuilder,您所做的就是附加字符串、字符等,然后调用 ToString() 方法。一个例子如下:

public String example()
{
    StringBuilder builder = new StringBuilder("Hello"); // initial value

    if (addSpaces)
    {
        builder.append(" ");
    }

    builder.append("World");
    return builder.ToString();
}

The purpose of a StringBuilder is to build a string right before it is consumed. They should be created fresh each time (this is good practice for the tougher problems that lie ahead). So instead of thinking about the stringbuilder class, think instead of how to build the string you need. Assuming you are storing the correct guesses in a char array, you can have three methods that build the string as needed. In one of those cases you shouldn't need a StringBuilder at all.

Your ToBlankFormat method could look like this:

public String toBlankFormat()
{
    char[] format = new char[answer.length()];
    Arrays.fill(format, '-');
    return new String(format);
}

Essentially, to use a StringBuilder all you do is append strings, characters, etc. and then call the ToString() method. An example would look like this:

public String example()
{
    StringBuilder builder = new StringBuilder("Hello"); // initial value

    if (addSpaces)
    {
        builder.append(" ");
    }

    builder.append("World");
    return builder.ToString();
}
温暖的光 2024-10-09 22:09:12

这三个 StringBuilder 应该是您的类的实例变量。您可以在类构造函数中实例化它们,也可以在选择正在猜测的单词时实例化它们。

The three StringBuilders should be instance variables of your class. You can instantiate them in your class constructor or you can instantiate them when you choose the word that is being guessed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文