gpt4 book ai didi

java - 比较 2 个单独数组中的 2 组整数值

转载 作者:行者123 更新时间:2023-12-01 14:13:11 25 4
gpt4 key购买 nike

我对java很陌生,并且遇到了这个我正在努力解决的问题。我有两组数字,存储在两个单独的数组中,代表彩票号码。第一组是用户号码,第二组是来自彩票网页的号码。我尝试比较数组中的数字位置,但我不确定什么结果能让我获得正确的比赛数量以及如何将比赛与奖金球包含在一起,因为彩票有 6 个用户号码,但抽奖中有 7 个彩票号码(6 个号码加一个奖金号码)。我在下面包含了我的代码:

     // set up an array to store numbers from the latest draw on the lottery web page
Integer [] numbers = new Integer [split.length];

int i = 0;
for (String strNo : split) {
numbers [i] = Integer.valueOf(strNo);
i++;
}

for (Integer no : numbers) {
System.out.println(no);
}

Element bonusElement = firstLottoRow.child(3);
Integer bonusBall = Integer.valueOf(bonusElement.text());
System.out.println("Bonus ball: " + bonusBall);
//Elements elementsHtml = doc.getElementsByTag("main-article-content");
final int SIZE = 7;
//array to store user numbers
int [] userNumbers = new int[SIZE];
boolean found = false;
int pos = 0;
int search = 0;
int searchPos=-1;
boolean bonus = false;
int lottCount;
while (pos<SIZE)
{
System.out.println("enter your numbers");
userNumbers[pos]=keyboard.nextInt();
pos++;
}
for (int count: userNumbers)
{
System.out.println(count);
}
while ((pos < SIZE) && (!found))
{
if (userNumbers[pos] == numbers[0])
{
found = true;
System.out.println("You have matched one number"); //am i wrong in saying //this?
}else pos++; //am i incrementing the wrong counter and at what point do i //implement the lottery counter?

}//while
if (!found)
{
System.out.println("You have not won this time");
}else if (userNumbers[pos] == bonusBall)
{
bonus = true; //i think this is wrong too
}
//how do i go about working out how many nos the player has matched or how many //numbers theyve matched plus the bonus?

最佳答案

我认为整数数组对于您的问题来说是错误的数据类型。相反,您应该使用 Set (当我这么说时,我假设每个数字都是唯一的)。完成此操作后,您要调用的方法将被称为 containsAll 。如果您的号码是唯一的,请改用列表。它还有一个 containsAll 方法。这段代码应该可以帮助您入门:

package com.sandbox;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Sandbox {

public static void main(String[] args) throws IOException {
Set<Integer> userNumbers = new HashSet<>();
userNumbers.add(1);
userNumbers.add(2);
userNumbers.add(3);
userNumbers.add(4);
userNumbers.add(5);
userNumbers.add(6);

Set<Integer> lotteryNumbers = new HashSet<>();
lotteryNumbers.add(1);
lotteryNumbers.add(2);
lotteryNumbers.add(3);
lotteryNumbers.add(4);
lotteryNumbers.add(5);
lotteryNumbers.add(6);
lotteryNumbers.add(7);

if (lotteryNumbers.containsAll(userNumbers)) {
System.out.println("We have a winner!");
} else {
System.out.println("Sorry, you're a loser");
}
}


}

关于java - 比较 2 个单独数组中的 2 组整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18322128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com