Stick to my java homework – using StringBuilder for Hangman games – help?
*Note: I didn't ask you to do my homework I'm just stuck
I'm designing a hangman course Obviously, we need three stringbuilders (a) one to show the hyphen: "–" the length of the word, b) one to show the correct letter guess: "– a – e -" and finally C) the other is basically the opposite of B (the guessed letter is replaced by hyphen and undisclosed letter) c) The purpose of is to see if there are any matches during guessing
My biggest problem is that I can't find many practical StringBuilder examples on Google, that is, my biggest problem is where I can / should instantiate three stringbuilders in the hangman class?
Thank you
Solution
I guess here that you have a hangman class as a model that can do three things (related to this). It is:
>Give you a string to guess each character in the word > provide you with a string to display the correctly guessed characters in the correct position > provide you with a string to display the used characters
These depend on the state of the model
>The word > character guessed
Based on this, I would say that you should have three methods to return strings and create a new StringBuilder instance in each method Constructing a string is separate from the state, just to clarify why I disagree with computerish
StringBuilder is a more efficient way to build strings and then only use concatenation, but it's easy to use First create an instance
StringBuilder builder = new StringBuilder();
Then build a string by appending a string or character (or other content):
builder.append('-'); builder.append('w');
After completion, construct a string instance from StringBuilder:
String string = builder.toString();
You end up with "- W", which is a rather boring example