我将如何匹配正确猜测的数量?

how would i match the number of correct guesses?

我想制作一个像彩票一样的程序,中奖号码是随机生成的。

如果用户猜对了所有 6 个数字,我使用 (guessednumbers.containsAll(winningnumbers)

如果用户只能猜5个数字,我应该用什么方法从随机生成的数字中算出5个正确的数字?

代码为:

System.out.println("The winning numbers were: " + winningNumbers);
System.out.println("Your numbers are: " + guessedNumbers);

guessedNumbers.retainAll(winningNumbers);
System.out.println("Your matched numbers are: " + guessedNumbers);

if (guessednumbers.containsAll(winningNumbers)); {
    System.out.println("Jackpot!");

}
else if (the supposed code if 5 digits were guessed correctly)
    System.out.println("You won a consolation prize!"); }

class 是 java.util.Listjava.util.Randomjava.util.ArrayList;

数据类型均为整型

由于您已经调用了 guessednumbers.retainAll(winningNumbers),所以正确猜中数字的数量正好是 guessedNumbers 的大小。

if (guessedNumbers.size() == 5)
    System.out.println("You have guessed exactly 5 numbers correct.");