好的,所以我在编程课上有这种练习,我们被要求做一个刽子手游戏,你可以看到。所有的程序都可以正常工作,除了一件事:当一个单词有一个重复不止一次的字母时,程序只接受一次,然后忽略其他的。我认为问题出在 setLetter 方法中。它的工作是用字母替换破折号。当我运行该程序并第二次输入该字母时,它确实告诉我这是一个正确的猜测,但它并没有用字母替换短划线。
任何帮助都将非常感谢! 谢谢。
import java.util.Random;
import java.util.Scanner;
public class Hangman {
public static void main(String [] args) {
// word list
String [] hangmanList = {"accelerate", "achieve", "able", "microscope",
"thumb", "wonderful", "yard", "wood", "lemon", "favorite", "decide", "circular",
"broke", "elephant", "funny", "hamburger", "leg", "job", "lighthouse", "mother",
"notebook", "puzzle"};
int allowedGuesses = 7;
String secretWord=pickRandomWord(hangmanList);
System.out.println(secretWord);
char[] correctGuesses = new char [secretWord.length()];
for (int i=0; i<correctGuesses.length;i++){
correctGuesses[i]= '-';
}
char [] incorrectGuesses= new char [20];
int mistakeCount = 0;
Scanner input = new Scanner(System.in);
for (int i=0; mistakeCount<allowedGuesses;i++){
if (checkWinner(correctGuesses)!=true){
printGallows(mistakeCount);
System.out.println("\n ");
for (int j=0;j<correctGuesses.length;j++){
System.out.print(correctGuesses[j]);
}
System.out.print("\nWrong guesses: ");
for (int j=0;j<=i;j++){
System.out.print(incorrectGuesses[j]);
}
System.out.print("\nPlease guess a letter: ");
char a = input.next().charAt(0);;
boolean letterFound = false;
letterFound = matchFound(a,secretWord);
if (letterFound) {
setLetter(a ,secretWord, correctGuesses);
System.out.println("-== correct guess! ==-");
}
else {
incorrectGuesses[i]=a;
mistakeCount++;
System.out.println(":(:(:( Oops! that is incorrect...");
}
}
else break;
}
System.out.println("\n********************************************");
System.out.println("The secret word was: " + secretWord);
if (checkWinner(correctGuesses)) {
System.out.println("Congratulations, you won!");
}
else {
System.out.println("Better luck next time!!!");
printGallows(mistakeCount);
}
}
private static boolean checkWinner(char[] correctGuesses) {
boolean isWinner=false;
for (int i=0; i<correctGuesses.length;i++){
if (correctGuesses[i]!= '-') isWinner=true;
else isWinner=false;
}
return isWinner;
}
public static boolean matchFound(char guess,String secretWord) {
char [] letters = secretWord.toCharArray();
for (int i=0; i<letters.length;i++){
if (guess == letters[i]) {
return true ;
}
}
return false;
}
public static boolean setLetter(char guess,String secretWord, char[] correctGuesses) {
char [] letters = secretWord.toCharArray();
int guessed=0;
for (int i=0;i<letters.length;i++){
if (guess==letters[i]){
guessed++;
}
if (guessed>0){
correctGuesses[i]=letters[i];
return true;
}
else if (guess==0){
correctGuesses[i]=letters[i];
return true;
}
}
return false;
}
private static void printGallows(int mistakeCount) {
if (mistakeCount==0){
System.out.print(" ____\n | |\n |\n |\n |\n |\n |\n |\n__|__"); }
else if (mistakeCount==1){
System.out.print(" ____\n | |\n | O\n |\n |\n |\n |\n |\n__|__"); }
else if (mistakeCount==2){
System.out.print(" ____\n | |\n | O\n | |\n |\n |\n |\n |\n__|__");}
else if (mistakeCount==3){
System.out.print(" ____\n | |\n | O\n | |\n | \\|\n |\n |\n |\n__|__");}
else if (mistakeCount==4){
System.out.print(" ____\n | |\n | O\n | |\n | \\|/\n |\n |\n |\n__|__");}
else if (mistakeCount==5){
System.out.print(" ____\n | |\n | O\n | |\n | \\|/\n | |\n |\n |\n__|__");}
else if (mistakeCount==6){
System.out.print(" ____\n | |\n | O\n | |\n | \\|/\n | |\n | /\n |\n__|__");}
else if (mistakeCount==7){
System.out.print(" ____\n | |\n | O\n | |\n | \\|/\n | |\n | / \\ \n |\n__|__");}
}
private static String pickRandomWord(String[] dictionary){
String secretWord=null;
Random r = new Random ();
secretWord = dictionary [r.nextInt(dictionary.length)];
return secretWord;
}
} // end of Hangman class
答案 0 :(得分:1)
是的,问题在于setLetter方法。你在第一封信比赛中回归真实。我的解决方案是
public static boolean setLetter(char guess, String secretWord, char[] correctGuesses) {
char[] letters = secretWord.toCharArray();
int guessed = 0;
for (int i = 0; i < letters.length; i++) {
if (guess == letters[i]) {
guessed++;
correctGuesses[i] = letters[i];
}
}
return guessed != 0;
}