java - 我无法理解收到的错误

标签 java

我似乎遇到了一个问题,由于某种原因,我在终端中的代码中出现错误,但似乎无法理解实际的问题。这是我收到的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Lottery.compareNumbers(LotteryApplication.java:59) at LotteryApplication.main(LotteryApplication.java:106)

这是我的代码:

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).");

        }
    }
}

最佳答案

在第二个 for 循环中,您在两者中都计算了 i++ 。因此,您可以在两个 for 循环中对 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;
                }
            }
        }
    }

关于java - 我无法理解收到的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28239119/

相关文章:

java - Eclipse java调试: source not found

java - 带有位置类的java树的实现

java - 针对不同实体的 CriteriaQuery

java - UI不会自动更新,只有刷新后才会改变

java - 在 Spring MVC 中使用 JSR-303 注释进行条件验证

java - 我可以在 Grails 中使用 @Async 注释吗

java - 我的电脑无法安装 Java

java - 使用配置文件构建 Maven 项目

java - 如何正确使 vim 缩进 java 注释?

java - Shazamcrest 定制搭配系列