1

我正在尝试制作两种方法,其他方法运行良好。问题是伪匹配计数器,其中输入应该是随机数,例如 5 4 3 2,用户猜测输入,例如 5 3 6 1,输出将是:伪匹配:1 和匹配:2。我不明白我的伪匹配方法哪里出错了。

public int match(int[] guess) //Counts the number of matches
{
    int count = 0;

    for(int i = 0; i<3; i++) 
    {
        if (lotteryNumbers[i] == guess[i])
        {
            count++;
        }

    }
    return count;
}
//First add psuedo counter for current program
//Then modify for multiple variables.

//Single slot to Single slot.
public int psuedoMatch(int[] guess)
{  
    boolean arraysEqual = true;
    int psuedoCount = 0;

    //Determine same size:
    if (lotteryNumbers.length != guess.length) 
    {
        arraysEqual = false;
    }

    //Determine if elements contain same data:

    while (arraysEqual && psuedoCount < 3) // guess.length
    {
        if (lotteryNumbers[psuedoCount] != guess[psuedoCount] )
        {
            arraysEqual = false;
        }
        psuedoCount++;
    }

    return psuedoCount;
}
4

2 回答 2

0

我认为以下是您正在尝试做的事情:

public int psuedoMatch(int[] guess) {          
    int psuedoCount = 0;

    //Determine same size:
    if (lotteryNumbers.length != guess.length) {
        return psuedoCount;
    }

    //Determine if elements contain same data:

    for(int i=0; i<lotteryNumbers.length || psuedoCount < 3; i++) {

        //I think you want to count all the matches till you have 3 matches
        if (lotteryNumbers[psuedoCount] == guess[psuedoCount]) {
            psuedoCount++;
        }            
    }

    return psuedoCount;
}
于 2012-10-24T03:06:30.817 回答
0

我猜您正在尝试查找这 2 个数组的匹配数,但是相等元素的相对索引应该不同,以便我们可以将它们添加到伪计数器机制中。所以看看这个方法并尝试一下:

public int psuedoMatch(int[] guess)
{
    int psuedoCount = 0;

    if (lotteryNumbers.length != guess.length)
        return 0;

    int size = lotteryNumbers.length;

    // Determine if elements contain same data, but at different indexes!

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            // Denote the inequality check i != j
            if (lotteryNumbers[i] == guess[j] && i != j)
                psuedoCount++;
        }
    }

    return psuedoCount;
}
于 2012-10-24T03:16:17.633 回答