2015-01-30 8 views
-1

나는 어떤 이유로 터미널의 코드에서 오류가 발생하지만 실제 문제를 이해할 수없는 문제가있는 것 같다. 스레드 "주요"java.lang.ArrayIndexOutOfBoundsException의나는 내가받는 오류를 이해할 수 없다.

예외 : 5 Lottery.compareNumbers (LotteryApplication.java:59) LotteryApplication.main (LotteryApplication.java에서 에서 여기에 내가지고있어 오류가 있습니다 : 106)이 내 코드

입니다 : 대한 루프 for(int j = 0; j < lotteryNumbers.length; i++)에서 for(int j = 0; j < lotteryNumbers.length; j++)에 내부

import java.util.Random; 
import java.util.Scanner; 

class Lottery { 

    /** 
    * The lottery numbers. 
    */ 
    private int lotteryNumbers[]; 

    /** 
    * Default Constructor. 
    * 
    * The class should use the Random class (from the Java API) to generate a 
    * random number in the range of 0 through 9 for each element in the array. 
    */ 
    public Lottery() { 
     Random rand = new Random(System.currentTimeMillis()); 
     lotteryNumbers = new int[5]; 
     for (int i = 0; i < lotteryNumbers.length; i++) { 
      lotteryNumbers[i] = rand.nextInt(10); 
     } 
    } 

    /** 
    * The class should also have a method that accepts an array of 5 integers 
    * that represent a person's lottery picks. The method is to compare the 
    * corresponding elements in the two arrays and return the number of digits 
    * that match. 
    */ 
    public int compareNumbers(int[] usersNumbers) { 
    int match = 0; 
    if (usersNumbers.length == lotteryNumbers.length) { 
     for (int i = 0; i < lotteryNumbers.length; i++) { 
      for(int j = 0; j < lotteryNumbers.length; i++) { 
       if (usersNumbers[i] == lotteryNumbers[j]) { 
        //match++; 
        break; 
       } 
      } 
     } 
    } 
    return match; 
    } 

    /** 
    * In addition, the class should have a method that returns a copy of the 
    * lotteryNumbers array. 
    */ 
    public int[] getLotteryNumbers() { 
     return lotteryNumbers; 
    } 
    } 

    /** 
    * Demonstrate the class in a program that asks the user to enter five numbers. 
    * The program should display the number of digits that match the randomly 
    * generate lottery numbers. If all of the digits match, display a message 
    * proclaiming the user a grand prize winner. 
    */ 
    public class LotteryApplication { 
    public static void main(String[] args) { 
     Lottery lottery = new Lottery(); 
     int lotteryNumbersCount = lottery.getLotteryNumbers().length; 

     System.out.println("\t\t\t\tLottery Application\n"); 
     System.out.println(" " + "There are " + lotteryNumbersCount 
       + " secret numbers in range of 0 through 9. " 
       + "Try to guess them!\n"); 

     // Asks the user to enter five numbers. 
     Scanner kb = new Scanner(System.in); 
     int numbers[] = new int[lotteryNumbersCount]; 

     for (int i = 0; i < numbers.length; i++) { 
      System.out.print(String.format("Enter Number %d: ", i + 1)); 
      numbers[i] = kb.nextInt(); 
     } 

     // Display the number of digits that match the randomly generate 
     // lottery numbers. 

     int match = lottery.compareNumbers(numbers); 

     if (match == lotteryNumbersCount) { 

      // If all of the digits match, display a message proclaiming the 
      // user a grand prize winner. 
      System.out.println("\nWOHOO! ALL CORRECT! YOU WON THE BIG PRIZE!"); 

     } else { 

      System.out.println("\nUh-oh! You hit " + match + " number(s)."); 

     } 
    } 
} 
+0

'compareNumbers' 메소드의'j' 루프에서'i'를 (를) 사용하고 있습니다. –

답변

1

을 초과하면 i++입니다. 따라서 두 for-loops에서 모두 i++으로 계산됩니다. 그러나 한 번만 i++으로 계산하고 다른 루프는 j++이어야합니다. 어쩌면 복사 붙여 넣기를 통해 발생했습니다.

if (usersNumbers.length == lotteryNumbers.length) { 
     for (int i = 0; i < lotteryNumbers.length; i++) { 
      //Here you have to set j++ 
      for(int j = 0; j < lotteryNumbers.length; j++) { 
       if (usersNumbers[i] == lotteryNumbers[j]) { 
        //match++; 
        break; 
       } 
      } 
     } 
    } 
+0

고마워요. 제가 어떻게 든 내 경기와 관련이 있다면, 어떻게 든 잘 못 됐습니다. –

+0

반갑습니다. 내 대답이 도움이 되었다면, 내 대답의 왼쪽에있는 '진드기'를 클릭하여 제 대답을 수락하십시오. 그렇게하면 다른 사람들이 답을 더 쉽게 찾을 수 있습니다. – Christian

+0

나는 그것을 투표 할 수 있으면 좋겠지 만 나는 15 명의 지지자를 필요로한다. 그러나 무관심은 당신에게 감사한다. –

1

변경 그렇지 않으면를 실행하는 것입니다,을 너무 많이 사용하고 두 번째 for-loop에 lotteryNumbers

관련 문제