被我的 Java 作业困扰 - 使用 StringBuilder 进行绞刑吏游戏 - 有帮助吗?
*注意:我不是要求你做我的作业。我只是被困住了。
我现在正在设计一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在这里猜测您有一个 Hangman 类,它作为一个模型执行三件事(与此相关),即:
这些都取决于模型的状态,这将是
基于此,我想说您应该有三种返回的方法字符串,并在每个方法中创建一个新的 StringBuilder 实例。构建字符串与状态是分开的,只是为了明确为什么我不同意 Computerish。
StringBuilder 是一种比仅使用连接更有效的构建字符串的方法,但它很容易使用。您首先创建它的一个实例。
然后你通过附加字符串或字符(或其他东西)来构建字符串:
完成后,你从 StringBuilder 构建一个 String 实例:
最后得到“-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:
These are all dependent on the state of the model which would be
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.
Then you build up the String by appending Strings or chars (or other things):
When you are done you construct a String instance from the StringBuilder:
and you end up with "-w" which is a rather boring example.
一个简短的例子:
是的,您可以创建任意数量的
StringBuilder
对象(假设您有无限的堆,但实际上是这样的:“您喜欢多少就多少”,它比 3 更大:- ) )A short example:
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 :-) )StringBuilder 的目的是在使用字符串之前构建字符串。每次都应该重新创建它们(这是解决即将出现的更棘手问题的良好实践)。因此,不要考虑 stringbuilder 类,而是考虑如何构建您需要的字符串。假设您将正确的猜测存储在 char 数组中,则可以使用三种方法根据需要构建字符串。在其中一种情况下,您根本不需要 StringBuilder。
您的
ToBlankFormat
方法可能如下所示:本质上,要使用 StringBuilder,您所做的就是附加字符串、字符等,然后调用
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: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:这三个 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.