-6

这就是输出的样子。

在此处输入图像描述

我需要在原始字符串中找到猜测的索引。

如果这是真的,那么它应该用字符串猜测中读取的字符替换索引处的问号。

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

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

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

我被困在 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);

        }
4

3 回答 3

2

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

  • .replace()返回 a ,除非您这样做,否则String它不会修改:option

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

  • 另外,由于您不想使用数组,我强烈建议您使用StringBuilder

编辑 1(基于来自重复线程的评论):
您可以使用 aStringBuilder来拥有一个初始化为 all 的字符串-。然后当有人猜出正确的字母时,您可以将 替换-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 = '-';

然后,在某人制作 a 之后guess,如果您确定 position 处的字符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);
       }
    }
}
于 2013-08-01T16:03:10.013 回答
1

这是我的看法,一个可玩的没有数组的刽子手游戏:

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!

就像其他人在这里所说的那样,这不是这样做的理想方式。数组会让这个更干净。

于 2013-08-01T16:25:05.000 回答
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
于 2013-08-01T15:41:38.303 回答