子手游戏:显示字母

时间:2019-12-10 12:31:37

标签: java

我被困在子手游戏中。 我正在尝试显示破折号中存在的字母,但我不知道该怎么办? 我需要一个数组吗?

    String word_to_search = "home";
    boolean[] found = new boolean[word_to_search.length()];
    String[] display = new String[word_to_search.length()];

    for(int i=0; i<word_to_search.length(); i++) {
      if ( found[i] ) {
         System.out.print(word_to_search.charAt(i));
      }
      else {
         System.out.print("_ ");
     }
   }

   System.out.println("");
   System.out.println("");

   for(int i=0;i<9;i++){
    System.out.print("Enter your letter : ");
    char user_input_letter = enter.next().charAt(0);
    if(word_to_search.indexOf(user_input_letter)>=0){
        System.out.println("The letter exists !!");
    }
    else{
        System.out.println("This letter does not exist ! ");
    }

   }

4 个答案:

答案 0 :(得分:1)

正如f1sh的评论所指出的那样,您不是在修改found数组:

if (word_to_search.indexOf(user_input_letter) >= 0) {
    found[i] = true;
    System.out.println("The letter exists !!");
}

另外,友善的提示:Java中的方法和参数通常使用lowerCamelCase命名,而不是lower_snake_case。

答案 1 :(得分:1)

我为您创建了一个简单的演示:

import java.util.Scanner;  
import java.util.ArrayList;
import java.util.List;

public class TestClass{

   public static void main(String args[])
   {  

       Scanner scan = new Scanner(System.in);
       StringBuffer word_to_search_hidden_stringBuffer = new StringBuffer("_ _ _ _");

       String word_to_search = "h o m e";     

       System.out.print(word_to_search_hidden_stringBuffer.toString());

       System.out.println("");
       System.out.println("");

       for(int i=0;i<9;i++){

        System.out.print("Enter your letter : ");

        char user_input_letter = scan.next().charAt(0);

        if(word_to_search.indexOf(user_input_letter)>=0){ 

        List<Integer> indexes = new ArrayList<>();

        for (int ii = 0; ii < word_to_search.length(); ii++) {
            if (word_to_search.charAt(ii) == user_input_letter) {
                indexes.add(ii);
            }
        }

        System.out.println("The letter exists !!");
        for(int indx: indexes) {
             word_to_search_hidden_stringBuffer.setCharAt(indx, user_input_letter);
        }

        System.out.println(word_to_search_hidden_stringBuffer);
        }
        else{

            System.out.println("This letter does not exist ! ");
        }

       }

   }

}  
  

输出:

     
     

输入您的字母:a

     

这封信不存在!

     

输入您的字母:h

     

这封信存在!!

     

h _ _ _

     

输入字母:z

     

这封信不存在!

     

输入您的字母:o

     

这封信存在!!

     

h _ _

     

输入您的字母:m

     

这封信存在!!

     

小时_

     

输入您的字母:e

     

这封信存在!!

     

时刻

     

输入您的字母:

当然,您当然需要重构此代码,并改进“游戏机制”,但是您想到了一种从源代码中显示找到的字母而不是“ _”的方法。

基本思想是可以方便地修改Stringbuffer,并且当用户找到字符时,我们就在这里:

    if(word_to_search.indexOf(user_input_letter)>=0){

            List<Integer> indexes = new ArrayList<>();   

            //---> find all the occurrences of the found character, and put theirs indexes into the List  

            for (int ii = 0; ii < word_to_search.length(); ii++) {
                if (word_to_search.charAt(ii) == user_input_letter) {
                    indexes.add(ii);
                }
            }

            System.out.println("The letter exists !!");
            for(int indx: indexes) {    

//----> change the respective chars at the indexes, now holding "_" to the correct char.  
                 word_to_search_hidden_stringBuffer.setCharAt(indx, user_input_letter);
            }  

    //---> reveal the latest word  
            System.out.println(word_to_search_hidden_stringBuffer);
            }

答案 2 :(得分:1)

此答案支持多个单词,单词之间也有空格,也不需要布尔数组

public class HelloWorld{

     public static void main(String []args){
   Scanner scan = new Scanner(System.in);

        System.out.println("Hello World");
        String word_to_search = "home";
    for(int i=0; i<word_to_search.length(); i++) {
     {
         System.out.print("_ ");
     }
   }

   System.out.println("");
   System.out.println("");

String dashed = null;
   for(int i=0;i<9;i++){
    System.out.print("Enter your letter : ");
    char user_input_letter = scan.next().charAt(0);
    if(word_to_search.indexOf(user_input_letter)>=0){
        dashed = showDashes(user_input_letter,dashed,word_to_search);
        System.out.println("The letter exists !!\n"+getSpaces(dashed));
    }
    else{
        System.out.println("This letter does not exist ! ");
    }

   }
     }


    public static String showDashes(char c ,String currentDashed,String original ){
        if(currentDashed == null ){
            currentDashed = "";
            for(int i=0; i<original.length(); i++) {
                currentDashed = currentDashed.concat("_");
            }
        }
        char[] chars = currentDashed.toCharArray();
        for(int i=0; i<original.length(); i++) {
            if (original.charAt(i)==c)
            {
                chars[i] = c;
            }
        }
        return String.copyValueOf(chars);
    }

    public static String getSpaces(String s){
        String with_spaces = "";
        for(int i = 0;i<s.length();i++){
            with_spaces= with_spaces .concat(s.charAt(i)+" ");
        }
        return with_spaces;
    }
}

答案 3 :(得分:1)

我建议使用StringBuilder来存储显示状态。 还要注意foundCount变量。它包含用来判断游戏是否完成的猜字母数量。

import java.util.Arrays;
import java.util.Scanner;

public class Hangman {
    public static void main(String[] args) {
        int foundCount = 0;
        Scanner enter = new Scanner(System.in);
        String word_to_search = "home";
        char[] pad = new char[word_to_search.length()];
        Arrays.fill(pad, '_');
        StringBuilder word_do_display = new StringBuilder();
        word_do_display.append(pad);

        System.out.println(word_do_display);
        System.out.println();

        for(int i=0;i<9;i++) {
            System.out.print("Enter your letter : ");
            char user_input_letter = enter.next().charAt(0);

            // Look for whether the entered letter exists in the word, and replace the placeholders with the occurrences of the letter
            boolean found = false;
            int letterIndex = -1;
            while ((letterIndex = word_to_search.indexOf(user_input_letter, letterIndex + 1)) >= 0) {
                found = true;
                foundCount++;
                word_do_display.setCharAt(letterIndex, word_to_search.charAt(letterIndex));
            }
            if (found) {
                System.out.println("The letter exists !!");
                System.out.println(word_do_display);
            } else {
                System.out.println("This letter does not exist ! ");
            }
            if (foundCount == word_to_search.length()) {
                System.out.println("You win!");
                break;
            }
        }
        if (foundCount < word_to_search.length()) {
            System.out.println("You lost!");
        }
    }
}