0
我對編程和Java非常新穎(正如你所看到的),並且在編程hang子手時遇到了一個基本問題。我的代碼並不是我所知道的最乾淨的,但我真的很想在我的錯誤邏輯明智的地方輸入一些信息。我的麻煩是讓程序在猜測中正確輸出'hangman display'。我認爲有一個問題,我嘗試了錯誤的++設置。我也無法得到重複的信件正常工作,但不能完全弄清楚。爪哇Hang子手不會正確地循環
import java.util.*;
public class HangMan
{
public static void main(String[] args)
{
char[] alphabet =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
String[] words =
{
"javascript", "declaration", "object", "class", "failing"
};
int randomIndex = (int) (Math.random() * words.length);
String randomWord = words[randomIndex];
char[] explodedWord = explode(randomWord);
char[] censoredWord = new char[randomWord.length()];
int triesWrong = 0;
Scanner input;
char letterGuess;
boolean letterFound = false;
for (int i = 0; i < randomWord.length(); i++)
{
censoredWord[i] = '_';
}
while (!gameWon(censoredWord, explodedWord))
{
hangMan(triesWrong, censoredWord, alphabet);
System.out.println("Please guess a letter: ");
input = new Scanner(System.in);
letterGuess = input.next().trim().charAt(0);
char[] repeatedLetters = new char[26];
for (int i = 0; i < alphabet.length; i++)
{
if (letterGuess == alphabet[i])
{
if(letterGuess == repeatedLetters[i])
{
System.out.println("You already guessed this letter, please enter another letter.");
}
else
{
repeatedLetters[i] = alphabet[i];
alphabet[i] = '_';
}
}
}
for (int i = 0; i < explodedWord.length; i++)
{
if (letterGuess == explodedWord[i])
{
censoredWord[i] = letterGuess;
letterFound = true;
}
}
if (!letterFound)
{
letterFound = false;
triesWrong++;
}
}
}
public static char[] explode(String bank) //getWord method for string bank
{
//create character array called explode that has length of string
char[] explodedWord = new char[bank.length()];
for (int i = 0; i < bank.length(); i++)
{
explodedWord[i] = bank.charAt(i);
}
return explodedWord;
}
public static void hangMan(int n, char[] dashWord, char[] alph)
{
if (n == 0)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 1)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 2)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 3)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 4)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 5)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 6)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
} else if (n == 7)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| /\\");
System.out.println("|");
System.out.println("|");
System.out.println("GAME OVER");
System.exit(0);
}
System.out.println("\n\n");
for (int i = 0; i < alph.length; i++)
{
System.out.print(alph[i] + " ");
}
System.out.println("\n\n");
for (int i = 0; i < dashWord.length; i++)
{
System.out.print(dashWord[i] + " ");
}
System.out.println("\n\n");
}
public static boolean gameWon(char[] dashWord, char[] bigWord)
{
if(dashWord.length != bigWord.length)
{
return false;
}
for(int i = 0; i < dashWord.length; i++)
{
if(dashWord[i] != bigWord[i])
{
return false;
}
}
System.out.println("Congratulations, you win!");
return true;
}
}
我會將hang子手數字存儲在文本文件中,這將顯着減少代碼量。並增加可讀性。 – martijnn2008