创建没有arrays的刽子手游戏

这就是输出应该是什么样子。

在此处输入图像描述

我需要在原始String中找到guess的索引。

如果这是真的,它应该用索引字符串中的char替换索引处的问号。

之后它应该取出字符串“abcdefghijklmnopqrstuvwxyz”中的字符串

如果originalString不包含猜测,那么它应该只取出字符串“abcdefghijklmnopqrstuvwxyz”中的字符串

我在谷歌上查了一下这个问题并发现了一堆代码,他们都在使用数组或我在课堂上没有学到的东西。 所以请不要使用数组。

我被困在if else声明中。

int count=1; while (count<=24){ Scanner keyboard = new Scanner(System.in); int length; String originalString; String guess; String option= "abcdefghijklmnopqrstuvwxyz"; String questionmarks; System.out.println("Please enter a string"); originalString=keyboard.nextLine(); length=originalString.length(); questionmarks = originalString.replaceAll(".", "?"); System.out.println("Original String: "+originalString); System.out.println("Guessed String: "+questionmarks); System.out.println("Characters to choose from: "+option); System.out.println("Please guess a character"); guess=keyboard.nextLine(); if (originalString.contains(guess)){ count++; } else{ option.replace(guess, "_"); count++; System.out.println(option); } 

我从粗略的一瞥中注意到的一些事情:

  • .replace()返回一个String ,它不会修改option除非你这样做:

    option = option.replace(guess, "_");

  • 此外,由于您不想使用Arrays,我强烈建议您使用StringBuilder

编辑1(基于来自重复线程的评论):
您可以使用StringBuilder来创建一个初始化为all的String。 然后当有人猜出一个正确的字母时,你可以用guess取代-

 StringBuilder sb_word = new StringBuilder(lengthOfOriginalString); for (int i = 0; i < length; i++) sb_word.append('-'); //add hyphens to StringBuilder, StringBuffer would also work 

你应该使用类似的东西:

 final char blank = '-'; 

然后,在有人guess ,如果你确定位置i的角色应该被guess替换,你可以这样做:

  sb_word.setCharAt(i, guess.charAt(0)); 

编辑2

 while (bodyparts > 0 && !win) //play game while you have bodyparts and haven't won { System.out.printf("Word to guess: %s\nEnter a letter or word guess: " , sb_word); guess = keyboard.next(); if (guess.length() == 1) { for (int i = 0; i < length; i++) //loop to see if guess is in originalString if (Character.toLowerCase(word.charAt(i)) == Character.toLowerCase(guess.charAt(0))) { //it is, so set boolean contains to be true and replace blank with guess sb_word.setCharAt(i, guess.charAt(0)); contains = true; } if (!contains) { bodyparts--; System.out.printf("Incorrect, you have %d bodyparts left.\n", bodyparts); } else if (sb_word.indexOf(String.valueOf(blank)) == -1) { //all the letters have been uncovered, you win win = true; System.out.println(word); } else { contains = false; System.out.println("Good guess."); } } else { if (guess.equals(word)) win = true; else { bodyparts = 0; System.out.printf("Incorrect, you have %d bodyparts left.\n" , bodyparts); } } } 

EDIT2:削减所有这些,但基本上,当你学习数组时,这将更容易。

编辑:与问题无关,但无论猜测是否成功,都不需要在你的块中发生条件? 你在两个实例中递增计数,你需要将猜测的字符清空,不管它是否存在,是否正确?

PSUEDOCODE用于“成功猜测”内部:

 String temporaryGuess = ""; for-loop for each character in originalString { if (character at current index = guess) append guess to temporaryGuess; else append a ? to temporaryGuess; } set the previous guessed String to the temporaryGuess String 

这是我对它的看法,一个没有数组的可玩的Hangman游戏:

 import java.util.Scanner; public class Hangman { private static Scanner scanner = new Scanner(System.in); private static String word; private static String availableChoices = "abcdefghijklmnopqrstuvwxyz"; private static String hiddenWord; private static boolean winner = false; public static void main(String[] args) { System.out.print("Enter a word to guess: "); word = scanner.nextLine(); hiddenWord = wordToQuestionMarks(word); System.out.println("Hangman Word Set: " + word + "\n\n"); while (!winner) { guessLetter(); } System.out.println("Congrats! You Win!"); } private static String wordToQuestionMarks(String word) { return word.replaceAll(".", "?"); } private static void guessLetter() { System.out.println("Hidden Word: " + hiddenWord); System.out.println("Characters to choose from: " + availableChoices); System.out.print("Guess a letter: "); String letterChoice = scanner.nextLine(); int found = 0; if (hasLetter(letterChoice)) { found = updateGameState(letterChoice); } updateAvailableChoices(letterChoice); System.out.println("You found " + found + " " + letterChoice + "\n"); gameOver(); } private static int updateGameState(String letter) { int found = 0; for(int i=0; i< word.length(); i++) { if (word.charAt(i) == letter.charAt(0)) { String prev = hiddenWord.substring(0,i).concat(letter); hiddenWord = prev.concat(hiddenWord.substring(i+1)); found++; } } return found; } private static void updateAvailableChoices(String removeLetter) { availableChoices = availableChoices.replace(removeLetter, " "); } private static void gameOver() { if (!hiddenWord.contains("?")) { winner = true; } } private static boolean hasLetter(String letter) { if (word.contains(letter)) { return true; } else { return false; } } } 

示例输出

 Enter a word to guess: stack Hangman Word Set: stack Hidden Word: ????? Characters to choose from: abcdefghijklmnopqrstuvwxyz Guess a letter: a You found 1 a Hidden Word: ??a?? Characters to choose from: bcdefghijklmnopqrstuvwxyz Guess a letter: z You found 0 z Hidden Word: ??a?? Characters to choose from: bcdefghijklmnopqrstuvwxy Guess a letter: s You found 1 s Hidden Word: s?a?? Characters to choose from: bcdefghijklmnopqr tuvwxy Guess a letter: t You found 1 t Hidden Word: sta?? Characters to choose from: bcdefghijklmnopqr uvwxy Guess a letter: k You found 1 k Hidden Word: sta?k Characters to choose from: bcdefghij lmnopqr uvwxy Guess a letter: c You found 1 c Congrats! You Win! 

像其他人在这里所说的那样,这不是理想的做法。 arrays会使这个更清洁。