我想使兩種方法,其他方法運行良好。麻煩是僞匹配計數器,其中輸入應該是隨機數,例如5 4 3 2,用戶猜測輸入例如5 3 6 1,輸出將是:僞匹配:1並匹配:2.我不理解我在我的僞匹配方法中出錯。Java匹配計數器和僞匹配計數器
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;
}
我們稱之爲「公牛和牛」遊戲=) – Juvanis
是我的解決方案對你有用嗎? – Juvanis