制作一个刽子手游戏

时间:2013-08-01 15:42:05

标签: java arrays string

逻辑: 这就是输出的样子。 http://prntscr.com/1is9ht我需要在orginalString中找到guess的索引。如果这是真的那么它应该用字符串guess中读取的字符替换索引处的问号。之后,它应该取出字符串“abcdefghijklmnopqrstuvwxyz”中的字符。

如果originalString不包含猜测,那么它应该只从字符串“ abcdefghijklmnopqrstuvwxyz ”中取出那个字符。我在google上查找了这个问题并发现了一堆代码,它们都使用了数组或者我在课堂上没有学过的东西。所以请不要使用数组。我被困在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);

    }

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

编辑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);
       }
    }
}