开始。我在 Java 中分配作业时遇到问题。我们被要求创建一个程序,用户需要从丢失的短语中猜测字母。我创建了这个短语并用 ? 替换了每个字符,但现在用户需要猜测。如果用户正确,我如何构造 for 循环以显示每个字符。这就是我迄今为止在 Eclipse 上所拥有的。
public static void main(String[]args)
{
Scanner stdIn = new Scanner(System.in);
String cPhrase = "be understaning";
char g;
boolean found;
String guessed;
System.out.println("\tCommon Phrase");
System.out.println("_ _ _ _ _ _ _ _ _ _ _ _ _");
System.out.println(cPhrase.replaceAll("[a-z]", "?"));
System.out.println(" ");
System.out.print(" Enter a lowercase letter guess: ");
g = stdIn.nextLine(); // I am trumped right here not sure what to do?
// Can't convert char to str
for (int i = 0; i < cPhrase.length(); ++i)
{
if( cPhrase.charAt(i) == g)
{
found = true;
}
if (found)
{
guessed += g;