【发布时间】:2017-12-11 06:22:38
【问题描述】:
作业是为 Evil Hangman 代码组合的几种方法。这个具体的方法赋值是:
遍历单词并返回一个数组,该数组包含单词中字母位置的字符代码,否则为0。
参数:
theWord - 程序选择的单词
letter - 用户输入的字母
guess - 目前猜到的字母
返回: 一个整数数组,如果用户正确猜到了 Word 中的一个字母,则将字符代码插入该位置。
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++;
}
我不确定我所采用的方法是否有效,因为在完成整个课程之前我无法检查。如果有人能告诉我它是否有任何问题,我将不胜感激!
【问题讨论】:
标签: java