0

任何人都可以请帮忙。我正在比较 2 组彩票号码,userNumbers 是您的彩票中的号码,而 numbers 数组表示网站上彩票中的号码。当我运行程序并输入 6 个正确数字时,程序会通知我我中了大奖。但是,如果我匹配的数字少于 6 个,我就会陷入一个 while 循环。任何人都可以帮助我摆脱这种情况,以便如果我匹配 2 个号码,程序会通知我我匹配了 2 个号码,如果我匹配 3 个号码,它会通知我这个等等!如果我匹配了奖金,程序会通知我我已经匹配了奖金和 (n) 个数字。我对Java很陌生,并且已经考虑了一段时间!提前致谢!

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;
         boolean jackpot;

         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 <SIZE; loop++ )
         {
            for (int loopOther = 0; loopOther < numbers.length; loopOther++)
            {
               if (userNumbers[loop] == numbers[loopOther])
               {
                  lottCount++;
               } else if (userNumbers[loop] == bonusBall)
                        {
                           bonus = true;
                        }
            }//for

         }//forMain

         if (lottCount == 6)
         {
            jackpot = true;
            System.out.println("You have matched all numbers!! Congratulations you are a jackpot winner");
         }else System.out.println(" You have matched " + lottCount + " numbers. Please visit the webpage did you see what you have won");
         while (lottCount < 6)

         if  (bonus)
           {
                  System.out.println("You have matched " + lottCount + " numbers " + "and" + " the bonus ball"
                          + bonusBall + " Please see the website to check your prize.");
           } else
                  System.out.println("You have not won at this time. ");
4

1 回答 1

1

看起来“while (lottCount < 6)”是不必要的,把它拿出来,它应该可以工作,看起来你想要一些类似的东西:

if (lottCount == 6) {
    System.out.println("You have matched all numbers!! Congratulations you are a jackpot winner");
} 
else if (!bonus) {
    System.out.println(" You have matched " + lottCount + " numbers. Please visit the webpage did you see what you have won");
}

if  (bonus) {
      System.out.println("You have matched " + lottCount + " numbers " + "and" + " the bonus ball"
          + bonusBall + " Please see the website to check your prize.");
}
于 2013-08-20T10:41:45.993 回答