替换数组中的值

时间:2016-08-05 23:38:36

标签: java arrays

我目前正在做一个小型自我项目来制作一个刽子手游戏。我想要它,所以计算机打印你必须找到的缺失字母作为下划线和你已经在隐藏字通常所在的地方找到的字母。我每次用户输入正确的char时,我都希望计算机用字母替换下划线。

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

public class HangmanGame {

    static String hiddenWord = "marcus";
    static int triesLeft = 6;
    static boolean done = false; 
    static int length = hiddenWord.length();
    static Scanner input = new Scanner(System.in);
    final static int maxLength = 20;
    static char[] letters = new char[20];
    static char[] repeatChecker = new char[20]; //this is to help prevent the user from putting the same char multiple times
    static char[] test = new char[20];
    static char[] fillWord = new char[length];
    static int var;
    static int var2;
    static char underscore = '_';




    public static boolean contains(char[] arr, char i) {
          for (char n : arr) {
             if (i == n) {
                return true;
             }
          }
          return false;
       }
    public static void stringToArray(String s){
        char[] stringArray = s.toCharArray();
        for (char output : stringArray){
            var2++;
            test[var2] = output;
            //System.out.println(test);

        }

    }
    public static void createSpaces(int n){
        for (int i = 0; i <= n-1; i++){
            fillWord[i] = underscore;
        }
        System.out.println(fillWord);
    }


    public static void arrayLetters(){
        System.out.println("Please enter a letter to guess: ");
        char charInput = input.next().charAt(0);
        for (int i = 0; i < length; i++){
            char wordLetter = hiddenWord.charAt(i);
            letters[i] = wordLetter;
            if (contains(letters, charInput)){
                if (contains(repeatChecker, charInput)){
                    System.out.println("You already did that word, try again!");
                    arrayLetters();
                }
                repeatChecker[var] = charInput;
                var++;
                fillWord[i] = fillWord[i].replace(charInput); 
                System.out.println("Nice! There is a " + charInput + " in this word!");
                System.out.println(repeatChecker);
                System.out.println("You have " + triesLeft + " tries left!\n");
                arrayLetters();

            }
            if (i == length-1){
                System.out.println("There is no " + charInput + " in this word!");
                triesLeft--;
                System.out.println("You have " + triesLeft + " tries left!\n");
                if (triesLeft <= 0){
                    System.out.println("You failed!");
                    System.exit(0);

                }
                arrayLetters();
            }
        }


    }

    public static void main(String[] args) {
        System.out.println("Welcome to my hangman game!");
        stringToArray(hiddenWord);
        //System.out.println(length);
        createSpaces(length);
        arrayLetters();
    }




}

如何正确使用replace()或其他函数,以便每当用户输入char时,我的代码都可以用字符替换下划线包含。

2 个答案:

答案 0 :(得分:1)

你必须写

fillWord[i] = charInput;

后两行

System.out.println(fillWord);

您打印的是repeatChecker,而不是fillWord

答案 1 :(得分:0)

你可以直接做:

fillWord[i] = charInput;

无论如何,数组都不需要替换方法。