这就是输出的样子。
我需要在原始字符串中找到猜测索引。
如果这是真的,它应该用索引替换索引处的问号 字符串猜测。
之后它应该取出字符串“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);
}
答案 0 :(得分:2)
我从粗略的一瞥中注意到的一些事情:
.replace()
会返回String
,除非您这样做,否则不会修改option
:
option = option.replace(guess, "_");
编辑1(基于来自重复帖子的评论):
您可以使用StringBuilder
将字符串初始化为所有-
。然后当有人猜出正确的字母时,您可以将-
替换为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);
}
}
}
答案 1 :(得分:1)
EDIT2:削减所有这些,但基本上,当你学习数组时,这将更加容易。
编辑:与问题无关,但是不管猜测是否成功,都不需要else
块中的条件?你在两个实例中递增计数,你需要将猜测的字符空白,不管它是否存在,是否正确?
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
答案 2 :(得分:1)
这是我对它的看法,一个没有阵列的可玩的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!
就像其他人在这里所说的那样,这不是理想的做法。阵列会使这个更清洁。