检查单词中的字母的方法(Hangman代码)

时间:2017-12-10 21:16:36

标签: java

赋值是为Evil Hangman代码组合的几种方法。这个具体的方法分配是:

继续单词并返回一个包含字母位置的字符的数组,否则为0。

参数:

theWord - 程序选择的单词

letter - 用户输入的字母

猜猜 - 到目前为止猜到的字母

返回: 一组int,如果用户正确猜到了字母中的一个字母,那么char代码就会插入该位置。

public static int[] checkLetterInWord(java.lang.String theWord, char letter, int[] guess) {
    int [] position = new int [guess.length];
    int correctGuesses=0;
    int incorrectGuesses=0;
    for (int i=0; i<position.length; i++) {
        for (int j=0; j<theWord.length(); j++) {
        if (theWord.charAt(j)==letter) {
            position[i]=j;
            correctGuesses++;
        }
        else if(theWord.charAt(j)!=letter) {
            position[i]=0;
            incorrectGuesses++;
        }

我不确定我的方法是否有效,因为在完成整个课程之前我无法检查。如果有人能告诉我它是否有任何问题我会很感激!

1 个答案:

答案 0 :(得分:-1)

使用列表

Bindings.createStringBinding(..)

使用数组

public static java.util.List<Integer> checkLetterInWord(final java.lang.String theWord, final char letter) {
    final java.util.List<Integer> returned = new java.util.ArrayList<Integer>();
    if (theWord != null) {
        for (int i = 0; i < theWord.length(); i++) {
            if (theWord.charAt(i) == letter) {
                returned.add(Integer.valueOf(i));
            }
        }
    }
    return returned;
}