Java编程做循环和库

Java编程做循环和库,java,Java,来吧。我在Java作业方面遇到了问题。我们被要求创建一个程序,用户需要从缺少的短语中猜出字母。我创建了这个短语,并将每个字符替换为?,但现在用户需要猜测。如果用户是正确的,我如何构造for循环来显示每个字符。这就是我到目前为止在eclipse上看到的 public static void main(String[]args) { Scanner stdIn = new Scanner(System.in); String cPhrase = "be understan

来吧。我在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;
你快到了


使用while而不是绑定到布尔条件的for循环。当且仅当单词中的所有字符都被猜测时,布尔值才会设置为false。

假设一次只猜测一个字符,则只需获取输入行的第一个字符即可

g = stdIn.nextLine().charAt(0);
要循环直到用户猜出整个短语,您需要在代码周围加上一段时间


这是一个快速解决方案。但请不要只是复制粘贴,你仍然需要理解它,因为我把内联评论

public static void main(String[] args)
{
    Scanner stdIn = new Scanner(System.in);

    String phrase = "be understanding";
    boolean[] guessed = new boolean[phrase.length()];
    // walk thru the phrase and set all non-chars to be guessed
    for (int i = 0; i < phrase.length(); i++)
        if (!phrase.substring(i, i + 1).matches("[a-z]"))
            guessed[i] = true;

    // loop until we break out
    while (true)
    {
        System.out.print("Please guess a char: ");
        char in = stdIn.nextLine().charAt(0);

        boolean allGuessed = true;
        System.out.print("Phrase: ");
        // walk over each char in the phrase
        for (int i = 0; i < phrase.length(); i++)
        {
            char c = phrase.charAt(i);
            // and check if he matches the input
            if (in == c)
                guessed[i] = true;
            // if we have an not already guessed char, dont end it
            if (!guessed[i])
                allGuessed = false;
            // print out the char if it is guessed (note the ternary if operator)
            System.out.print(guessed[i] ? c : "?");
        }
        System.out.println("\n------------------");
        // if all chars are guessed break out of the loop
        if (allGuessed)
            break;
    }

    System.out.println("Congrats, you solved the puzzle!");

    stdIn.close();
}

这不是问题。您的代码示例不完整-大括号未关闭。想必你至少有可以编译的东西?请发布一个完整的,可以运行的代码。在Eclipse中将代码发布到这里之前,只需按Ctrl+Shift+F并选中所有内容,即可格式化代码。很好地说明了另一个问题+1当我再次获得投票时马上出现:D这里有一个例子,请参考它。for循环是检查短语中是否包含字符,而不是循环猜测。@Rouby Good catch!对过一段时间可能会包装现有的impl。不要忘记初始化boolean found和String Guessed变量。非常感谢。这很有帮助。
public static void main(String[] args)
{
    Scanner stdIn = new Scanner(System.in);

    String phrase = "be understanding";
    boolean[] guessed = new boolean[phrase.length()];
    // walk thru the phrase and set all non-chars to be guessed
    for (int i = 0; i < phrase.length(); i++)
        if (!phrase.substring(i, i + 1).matches("[a-z]"))
            guessed[i] = true;

    // loop until we break out
    while (true)
    {
        System.out.print("Please guess a char: ");
        char in = stdIn.nextLine().charAt(0);

        boolean allGuessed = true;
        System.out.print("Phrase: ");
        // walk over each char in the phrase
        for (int i = 0; i < phrase.length(); i++)
        {
            char c = phrase.charAt(i);
            // and check if he matches the input
            if (in == c)
                guessed[i] = true;
            // if we have an not already guessed char, dont end it
            if (!guessed[i])
                allGuessed = false;
            // print out the char if it is guessed (note the ternary if operator)
            System.out.print(guessed[i] ? c : "?");
        }
        System.out.println("\n------------------");
        // if all chars are guessed break out of the loop
        if (allGuessed)
            break;
    }

    System.out.println("Congrats, you solved the puzzle!");

    stdIn.close();
}