创建没有数组的刽子手游戏[关闭]

2024-01-27

这就是输出的样子。

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

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

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

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

我在Google上查了这个问题,发现了一堆代码,它们都使用了数组或者我在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);

        }

我粗略地注意到了一些事情:

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

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

  • 另外,由于您不想使用数组,所以我强烈建议您使用字符串生成器 http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

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

EDIT 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);
       }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建没有数组的刽子手游戏[关闭] 的相关文章

随机推荐