我对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?

最佳答案

首先,您需要一种比较前6个整数的方法。顺序无关紧要,因此您需要检查乐透号码是否与您机票上的任何号码相匹配。最简单的方法是在for循环中使用for循环。每次检查乐透号码是否与您的六个号码中的任何一个匹配,然后对照您的六个号码检查下一个乐透号码。如果您有比赛,增加一个计数器
int count = 0; //在循环外初始化
if(myNum [x] == lottoNum [x]){
计数=计数+ 1; }

现在您将知道前六个数字中有多少个是匹配的。现在创建另一个方法,以查看奖金是否匹配。这只是
布尔奖金
if(myNums [7] == lottoNum [6]){
bonusHit = true;}
其他{
bonusHit = false;}

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

10-10 13:59
查看更多