我正在比较来自2个独立数组的2组整数值

时间:2013-08-20 10:33:32

标签: java arrays loops

任何人都可以帮忙。我正在比较2套彩票号码,userNumbers是你机票上的数字,而数字数字代表网站上彩票抽奖的数字。当我运行程序并键入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. ");

1 个答案:

答案 0 :(得分:1)

看起来像“while(lottCount&lt; 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.");
}