import java.util.Scanner;
public class Program09
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter the phrase to be guessed at: ");
String cp = stdIn.nextLine();
// I added the 'revealed' variable, and it seemed to help somewhat.
char [] tmpArr = new char [cp.length()];
int spaces = initTemplateArray(cp, tmpArr);
int cons = 0;
int vowel = 0;
int revealed = 0;
boolean running = true;
printTemplateArray(tmpArr);
do
{
char c = getConsonant(stdIn);
cons += updateTemplateArray(tmpArr, cp, c);
revealed += updateTemplateArray(tmpArr, cp, c);
printTemplateArray(tmpArr);
System.out.println();
if (cons >= 1)
{
char v = getVowel(stdIn);
vowel += updateTemplateArray(tmpArr, cp, v);
revealed += updateTemplateArray(tmpArr, cp, v);
printTemplateArray(tmpArr);
System.out.println();
}
if (revealed == tmpArr.length - spaces)
{
System.out.println("\n\nCongratulations!\n\nThe phrase contained " + cons + " consonants, " + vowel + " vowels, and " + spaces + " spaces.");
running = false;
}
} while (running);
stdIn.close();
}
//Most of this method was provided for us by the instructor:
public static int updateTemplateArray (char [] tmpArr, String sPhrase, char guess)
{
int revealed = 0;
for (int i = 0; i < tmpArr.length; ++i)
{
if (sPhrase.charAt(i) == guess)
{
tmpArr[i] = guess;
++revealed;
}
}
return revealed;
}
}
我遇到了在主方法中结束游戏并打印结果的问题。该程序偶尔会起作用(例如当你使用&#34;地狱&#34;这个词时要猜到)。换句话说,当这个词被揭示时(它不会结束游戏),它将继续询问辅音和元音,或者在完全显示该词之前它将结束游戏。
我怀疑我的错误在于我将数组中的辅音和元音的数量与我的do / while循环中数组中的字符数进行比较。
我在这里搜索了其他的刽子手问题,并找到了提交的内容,但似乎都没有涉及这个特定问题。抱歉长度。