如何为我的hangman java代码添加更多功能?
我创建了这段代码,它是一个简单的刽子手游戏。该代码检查用户输入是字符还是字符串,并将破折号数组中的下划线与用户输入交换 我的问题是如何添加功能来告诉用户他们的输入不在单词中,以及如果用户输入例如单词中的两个或更多连续字母,如何交换如何为我的hangman java代码添加更多功能?,java,Java,我创建了这段代码,它是一个简单的刽子手游戏。该代码检查用户输入是字符还是字符串,并将破折号数组中的下划线与用户输入交换 我的问题是如何添加功能来告诉用户他们的输入不在单词中,以及如果用户输入例如单词中的两个或更多连续字母,如何交换 public class question6 { public static void main(String[] args) { Scanner input = new Scanner (System.in); Strin
public class question6 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String secretword = "testing";
// now we need to create an array that stores characters so that we can swap them
// this is an array of characters that has the same length as the secret word
char[] dashes =new char[secretword.length()];
// now we need to add dashes to the array
for (int i=0; i<secretword.length(); i++){
dashes[i]= '_' ;
}
// now we need to start a loop that asks the user for input until all the dashes are swapped
// declaring variables for loop counter and steps
int counter = 0;
int steps =0;
// condition remains true until the counter is the same length as the secret word to make sure we swap everything
while (counter<secretword.length()){
// asking for input
System.out.print("Key in one character or your guess word: ");
String userinput = input.nextLine();
// if it is a character
if (userinput.length() == 1){
for (int i = 0; i<secretword.length(); i++){
// swapping
if (userinput.charAt(0) == secretword.charAt(i)){
dashes[i] = userinput.charAt(0);
counter ++;
}
}
}
// swapping the whole word
else if (userinput.equals(secretword)){
for (int j=0; j<secretword.length(); j++){
dashes[j]= userinput.charAt(j);
counter = secretword.length();
}
}
steps ++;
System.out.println(dashes);
}
}
}
以下是您的代码的修订版本,可满足您的需要:
public class HangManGame {
private static final int MAX_GUESSES = 10;
private String secretword;
private char[] dashes;
int guessedPositions;
int guesses;
public static void main(String[] args) {
HangManGame game = new HangManGame("testing");
game.start();
}
private HangManGame(String secretword) {
this.secretword = secretword;
initDashes();
}
private void initDashes() {
dashes = new char[secretword.length()];
for (int i=0; i < secretword.length(); i++){
dashes[i]= '_' ;
}
}
private void start() {
Scanner input = new Scanner (System.in);
while (guessedPositions < secretword.length() && guesses < MAX_GUESSES) {
System.out.println("Guess the word: " + Arrays.toString(dashes));
guessedPositions += guess(input.nextLine());
guesses++;
}
reportEndOfGame();
}
private int guess(String userInput) {
int guessedPositionsThisTurn = 0;
for (int i = 0; i < userinput .length(); i++) {
guessedPositionsThisTurn += guessLetter(userinput.charAt(0));
}
reportIfAllWrong(guessedPositionsThisTurn);
return guessedPositionsThisTurn;
}
private void guessLetter(char userChar) {
int guessedPositionsThisLetter = 0;
for (int i = 0; i < secretword.length(); i++) {
if (secretword.charAt(i) == userChar){
dashes[i] = userChar;;
guessedPositionsThisLetter++;
}
}
return guessedPositionsThisLetter;
}
private void reportIfAllWrong(int guessedPositionsThisTurn) {
if (guessedPositionsThisTurn == 0) {
System.out.println("All letters were wrong!");
}
}
private void reportEndOfGame() {
if (guessedPositions == secretword.length()) {
System.out.println("Yay! You guessed the word");
}
else {
System.out.println("You failed! Any last words?");
}
}
}
注意,main方法创建一个HangMan实例,并调用它的start方法来玩游戏。HangMan实例具有存储游戏状态的字段,可以从我创建的不同方法中引用这些值。将代码拆分为方法使其更具可读性,因为整个过程被划分为逻辑子任务。我认为您高估了空行的价值。like和in中有一些有趣的文章。我强烈推荐它们,因为它们有助于学习如何提出好的问题。