0

逻辑: 这就是输出的样子。http://prntscr.com/1is9ht我需要在原始字符串中找到猜测的索引。如果这是真的,那么它应该用字符串猜测中读取的字符替换索引处的问号。之后,它应该从字符串“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);

    }

请建议我一些没有为我的问题实现数组概念的代码,任何帮助将不胜感激

4

1 回答 1

0

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

  • .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-01T15:49:15.880 回答