所以基本上我正在编写一个彩票程序,使用两个树集,每个树集有 6 个整数,一个用户输入,另一个随机生成。我必须将数字相互比较才能输出奖品,例如,如果两个数字匹配,那么您将赢得 100 美元。我在如何创建这样的方法时遇到了麻烦。任何帮助都会非常感谢!
这是我的主要内容,
public static void main(String[] args)
{
Set<Integer>s1 = createWinningNumber();
Set<Integer>s2 = getUserNumber();
System.out.println("Your ticket was: "+s2);
System.out.println("Winning numbers: "+s1);
}//End of main
public static Set<Integer> getUserNumber()
{
int set = 0;
Set<Integer> number = new TreeSet<Integer>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Pick your Lucky Lotto Numbers");
System.out.print("Type 6 lotto numbers: ");
for(int i = 0; i< 6;i++)
{
System.out.print("");
set = keyboard.nextInt();
number.add(set);
}
return number;
}//End of getUserNumber
public static Set<Integer> createWinningNumber()
{
Set<Integer> winning = new TreeSet<Integer>();
Random generator = new Random();
for(int i = 0;i<6;i++)
{
winning.add(generator.nextInt(40));
}
return winning;
}//End of createWinningNumber
到目前为止的输出
Pick your Lucky Lotto Numbers
Type 6 lotto numbers: 4 12 7 35 20 1
Your ticket was: [1, 4, 7, 12, 20, 35]
Winning numbers: [5, 8, 19, 24, 32]