0

我有一个刽子手项目的逻辑问题,它需要用户的一封信并搜索该信是否包含在密码中。问题是我的编程方式,如果用户在密码中猜到的字母多次出现。它只会通过并表示它们。这不是我想要的,我只希望它一次更新正确猜测的字母的状态。

我尝试了一些不同的东西,比如在之后设置一个中断,status(guessCh,但是迭代器只会转到字母匹配的第一次出现并停在那里。

对此有什么简单的解决方法吗?

private void compare(String str)
{
    guessCh = str.charAt(0);
    char secretCh = '0';
    for (int i = i2; i < secretWord.length(); i++)                                           // Cuts the secret word into individual chars to process. 
    {
        secretCh = secretWord.charAt(i);
//      Compare the two strings.
        if (guessCh == secretCh)                                                            
        {
            status(guessCh, i);                                                               // Sends the letter & placement to status().  
        }

    }

}

n

private String status(char guessCh, int placement)
{
/*  Update and return status. */

    if (guessCh >='A' && guessCh <= 'Z')
    {
        status = new StringBuffer(status).deleteCharAt(placement).toString();
        status = new StringBuffer(status).insert(placement,guessCh).toString();
        println("That guess is correct.");
        canvas.displayWord(status);
        return status;
    }

    return status;
}
4

3 回答 3

2

您可以使用比较方法中的状态变量测试先前的解决方案。

    if (guessCh == secretCh && status.charAt(i) != secretCh)
    {
        status(guessCh, i);
        break;
    }
于 2012-09-30T20:17:33.447 回答
1

据我所知(和理解),您面临的基本问题是由for比较方法中的循环引起的。

(注意,我的示例区分大小写,您需要考虑到这一点)

我可以建议两种基本方法...

第一个是,通过一次检查匹配所有出现...

private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;

public TestStringCompare() {

    secretWord = "This is a test";
    // This is a copy of the secret word, this ensures that
    // we always have a copy of the original.
    secretBuffer = secretWord;
    status     = "______________";
    guessCh = 'i';

    compare("i");

}

private void compare(String str) {

    while (secretBuffer.contains(str)) {

        int foundAt = secretBuffer.indexOf(str);
        status(str.charAt(0), foundAt);
        // We want to remove the "guess" from our check string
        // so it doesn't cause a false positive in the future
        StringBuilder sb = new StringBuilder(secretBuffer);
        sb.replace(foundAt, foundAt + 1, "_");

        secretBuffer = sb.toString();
        System.out.println(secretBuffer);


    }

}

private String status(char guessCh, int placement) {
    /*  Update and return status. */

    if (Character.isLetter(guessCh)) {
        status = new StringBuffer(status).deleteCharAt(placement).toString();
        status = new StringBuffer(status).insert(placement, guessCh).toString();
        System.out.println("That guess is correct.");
        System.out.println(status);
    }

    return status;
}

这会产生:

That guess is correct.
__i___________
Th_s is a test
That guess is correct.
__i__i________
Th_s _s a test

或者,替换第一次出现的猜测(据我所知,这是你所追求的)

public class TestStringCompare {

    public static void main(String[] args) {
        new TestStringCompare();
    }
    private char guessCh;
    private String secretWord;
    private String status;
    private String secretBuffer;

    public TestStringCompare() {

        secretWord = "This is a test";
        secretBuffer = secretWord;
        status     = "______________";
        guessCh = 'i';

        compare("i");

    }

    private void compare(String str) {

        if (secretBuffer.contains(str)) {

            int foundAt = secretBuffer.indexOf(str);
            status(str.charAt(0), foundAt);
            // Some where here you need to remove the "guess" character
            // to ensure that it doesn't get repeated...
            StringBuilder sb = new StringBuilder(secretBuffer);
            sb.replace(foundAt, foundAt + 1, "_");

            secretBuffer = sb.toString();
            System.out.println(secretBuffer);


        }

    }

    private String status(char guessCh, int placement) {
        /*  Update and return status. */

        if (Character.isLetter(guessCh)) {
            status = new StringBuffer(status).deleteCharAt(placement).toString();
            status = new StringBuffer(status).insert(placement, guessCh).toString();
            System.out.println("That guess is correct.");
            System.out.println(status);
        }

        return status;
    }
}

哪个会产生这个...

That guess is correct.
__i___________
Th_s is a test
于 2012-09-30T20:51:08.293 回答
0

I am guessing this is homework of some sort - but anyway, why not use indexOf

于 2012-09-30T20:07:41.633 回答