java - 我正在比较两个单独数组中的两组整数以匹配彩票号码

标签 java arrays

我的循环有问题,我意识到可能需要进行一些轻微的调整才能使其正常工作,但我只是看不到那是什么!我已包含以下代码:

final int SIZE = 6;
         //array to store user numbers
         int [] userNumbers = new int[SIZE];
         boolean found = false;
         int pos = 0;
         boolean bonus = false;
         int lottCount = 0;

         while (pos<SIZE)
         {
            System.out.println("enter your numbers");
            userNumbers[pos]=keyboard.nextInt();
            pos++;
         }
         for (int count: userNumbers)
         {
            System.out.println(count);
         }

         for (int loop = 0; loop <numbers.length; loop++ )
         {
            for (int loopOther = 0; loopOther < SIZE; loopOther++)
            {
               if (userNumbers[loop] == numbers[loopOther])
                  lottCount++;
            }
               if (userNumbers[loop] == bonusBall)
               {
                  bonus = true;
                  System.out.println("You have matched " + lottCount + " numbers " + "and" + " the bonus ball" + bonusBall);
               }
               else 
                  {
                  System.out.println("You have not won at this time");
                  }
         }

         System.out.println("You have matched " + lottCount + " numbers");

输出如下所示:

15
16
17
18
19
43
You have not won at this time
You have not won at this time
You have not won at this time
You have not won at this time
You have not won at this time
You have matched 1 numbers  the bonus ball43
You have matched 1 numbers

我只希望程序将每种情况通知我一次。谁能帮我这个?提前致谢

最佳答案

     for (int loop = 0; loop <numbers.length; loop++ )
     {
        for (int loopOther = 0; loopOther < SIZE; loopOther++)
        {
           if (userNumbers[loop] == numbers[loopOther])
              lottCount++;
        }
        if (userNumbers[loop] == bonusBall)
        {
           bonus = true;
        }
     }
     if (bonus)
     {
         System.out.println("You have matched " + lottCount + " numbers " + "and" + " the bonus ball" + bonusBall);
     }
     else 
     {
         System.out.println("You have not won at this time");
     }

或更短:

     for (int number : numbers)
     {
        for (int userNumber : userNumbers)
        {
           if (userNumber == number)
              lottCount++;
        }
        if (userNumber == bonusBall)
        {
           bonus = true;
        }
     }
     if (bonus)
     {
         System.out.println("You have matched " + lottCount + " numbers " + "and" + " the bonus ball" + bonusBall);
     }
     else 
     {
         System.out.println("You have not won at this time");
     }

关于java - 我正在比较两个单独数组中的两组整数以匹配彩票号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18329399/

相关文章:

Java Mac HMAC 与 C++ OpenSSL hmac

java - 错误 Java sql.SQLException : Parameter index out of range (1 >)

java - 如何知道对象与数据库相比发生了变化

java - 测试当指定文件不存在时应引发异常

java - TargetNamespace 与 Namespace 有何不同

javascript - 如何在带有子项的 JSON 对象上制作 map

c++ - 使用数组名称时的异常

sql - 比较 postgres 中的数组

c++ - 字符串矩阵的 sizeof

javascript - 对数组使用 for ...of 可以吗?