创建没有阵列的刽子手游戏 [英] Creating hangman game without arrays

查看:104
本文介绍了创建没有阵列的刽子手游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是输出的样子。

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

i need to find the index of guess in the original String.

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

If that's true than it should replace the question mark at the index with the char read in the string guess.

之后它应取出来自字符串abcdefghijklmnopqrstuvwxyz

After that it should take out that char from the string "abcdefghijklmnopqrstuvwxyz"

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

If originalString doesn't contain guess than it should only take out that char from the string "abcdefghijklmnopqrstuvwxyz"

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

I looked up this question on Google and found a bunch of codes, they were all using arrays or something i have not learned in the class es. So please don't use arrays.

我被卡在if else语句中。

I am stuck at the if else statement.

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

        }


推荐答案

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

A few things that I noticed from a cursory glance:


  • .replace()返回字符串,除非你这样做,否则不会修改选项

  • .replace() returns a String, it will not modify option unless you do:

option = option.replace(guess,_);

编辑1(基于来自重复帖子的评论):

您可以使用 StringBuilder 来将一个String初始化为所有 - 。然后当有人猜出正确的字母时,您可以用 guess 替换 -

EDIT 1 (based off of comment from duplicate thread):
You can use a StringBuilder to have a String that's initialized to all -. Then when someone guess a correct letter, you can replace the - with the 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

你应该使用类似的东西:

You should really use something like:

final char blank = '-';

然后,在有人做出 guess 之后,如果你确定位置 i 的字符应该被 guess 替换,你可以这样做:

Then, after someone makes a guess, if you've determined that the character at position i should be replaced by guess, you could do:

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

这篇关于创建没有阵列的刽子手游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆