可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
public class Hangman {
private String secret;
private String disguise;
private int guessCount;
private int wrong;
public Hangman() {
secret="word";
disguise="????";
guessCount=0;
wrong=0;
}
public void makeGuess(char input) {
String temp;
temp=disguise;
for (int i=0; i<secret.length(); i++) {
if (secret.charAt(i)==input) {
disguise=disguise.replace(disguise.charAt(i), input);
}
}
if (temp.equals(disguise))
wrong++;
}
I'm having some difficulty with my code, specifically the disguise=disguise.replace line. The objective of my code is to replace the symbol in disguise with the letter in secret via guessing from the user. The for loop goes through all of the letters in the secret word and looks for matches between the user inputted character and the letters in the secret word.
If there is a match, I want the program to replace the symbol in disguise at that position with the character from input.
Instead what's happening is my code is replacing all the letters in disguise with the letter that the user guesses if it's in the word secret.
Example:
????
w
wwww (disguise)
word (secret)
what I want:
????
w
w???
word
This is my demo class:
import java.util.Scanner;
public class HangmanDemo {
public static void main(String[] args) {
char input;
Hangman game = new Hangman();
Scanner keyboard = new Scanner(System.in);
System.out.println(game.getDisguisedWord());
for (int i=0;i<10;i++){
String line=keyboard.nextLine();
input = line.charAt(0);
game.makeGuess(input);
game.guessCount();
game.getDisguisedWord();
game.isFound();
System.out.println(game.getDisguisedWord());
System.out.println(game.getSecretWord());
}
}
}
If anyone could point out what is wrong with my replace statement in the class coding, that would be greatly appreciated.
Thanks
回答1:
Since your "disguise" contains only ?
and replace(char oldchar, char newChar)
actually replaces all occurrences of oldchar
, your whole string is being replaced.
What you really want is to replace a character at a specific position, and for that you could use StringBuilder#setCharAt
. See my example below:
public void makeGuess(char input) {
StringBuilder temp = new StringBuilder(disguise);
boolean wrongGuess = true;
for (int i=0; i<secret.length(); i++) {
if (secret.charAt(i) == input) {
temp.setCharAt(i, input);
wrongGuess = false;
}
}
disguise = temp.toString();
System.out.println(disguise);
if (wrongGuess){
wrong++;
}
}
回答2:
Your disquise word is all ???? so no matter what you input, the code disguise.charAt(i)
will always be a ?
so on every ? will be replaced
What you want is secret.charAt(i)
like below
public void makeGuess(char input) {
String temp;
temp=disguise;
char[] disquiseChars = disquise.toCharArray(); //added
for (int i=0; i<secret.length(); i++) {
if (secret.charAt(i)==input) {
//disguise=disguise.replace(secret.charAt(i), input);
//Change in above line, but this still wont work, try this now
disquiseChars[i] = input;
}
}
disquise = disquiseChars;
if (temp.equals(disguise))
wrong++;
}
Not the most elegant solution but should do the trick
Edit: Below would be a quick way to check if they have already guess a letter, but only guessed a correct letter I realise now
public void makeGuess(char input) {
if(diquise.contains(String.valueOf(input))
{
System.out.println("You have already guessed " + input);
wrongGuess++; // if you decide
return;
}
String temp;
temp=disguise;
char[] disquiseChars = disquise.toCharArray();
for (int i=0; i<secret.length(); i++) {
if (secret.charAt(i)==input)
{
disquiseChars[i] = input;
}
}
disquise = disquiseChars;
if (temp.equals(disguise))
wrong++;
}
回答3:
String chartAt(i) return character not index. If you replace the character than all character occurance will be replaced.
If you go through the Java Documentation then you have to learn why replace(char, char) method does not replace single character. Following qoute is from Java Docs.
public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of
oldChar in this string with newChar. If the character oldChar does not
occur in the character sequence represented by this String object,
then a reference to this String object is returned. Otherwise, a new
String object is created that represents a character sequence
identical to the character sequence represented by this String object,
except that every occurrence of oldChar is replaced by an occurrence
of newChar.
回答4:
Just replace the Replace function in the loop with a disguise[i] = input;
and as the loop continues, all the changes are made as needed :)
The issue here is as Java Devil suggested, you're replacing all the '?' with the input!
Edit : Sorry I was thinking .NET when I said disguise[i]
.
Either declare Disguise as a StringBuilder
as use disguise.setCharAt(i, input);
or Do
disguise = disguise.substring(0,i) + input + disguise.substring(i+1, disguise.length());
The 2nd version works for strings too. You are creating 2 substrings first from 0 to the index of the position you want to change and then adding the new character & concatenating the 2nd substring from i+1 to the end
Hope it helps..
Cheers
回答5:
Take a look at the String.replace(char,char) docs and you will see that it replaces EVERY instance of the oldChar with the newChar. This means that every '?' will be replaced by 'w'. What you want to do instead is replace only the character at that given position.
To do this, you can use a char[] instead of a string and easily replace characters by index. To get a char[] from a String, simply call the String.toCharArray() method on the secret word.
public class Hangman {
private String secret;
private char[] disguise;
private int guessCount;
private int wrong;
public Hangman() {
secret="word";
disguise="????".toCharArray(); // <-- Basically this changes
guessCount=0;
wrong=0;
}
public void makeGuess(char input) {
boolean found = false;
for (int i=0; i<secret.length(); i++) {
if (secret.charAt(i)==input) {
disguise[i] = input; // <-- Basically this changes
found = true;
}
}
if (!found)
wrong++;
}