Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我能';我不明白我收到的错误_Java - Fatal编程技术网

Java 我能';我不明白我收到的错误

Java 我能';我不明白我收到的错误,java,Java,我似乎有一个问题,在代码中,在终端中,出于某种原因,我得到了一个错误,但似乎无法理解实际的问题。下面是我得到的错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:5 在lotkety.compareNumbers(LotteryApplication.java:59) 位于LotteryApplication.main(LotteryApplication.java:106) 这是我的代码: import java.util.Rand

我似乎有一个问题,在代码中,在终端中,出于某种原因,我得到了一个错误,但似乎无法理解实际的问题。下面是我得到的错误:

线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:5 在lotkety.compareNumbers(LotteryApplication.java:59) 位于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).");

        }
    }
}
import java.util.Random;
导入java.util.Scanner;
班级彩票{
/**
*彩票号码。
*/
私人国际彩票号码[];
/**
*默认构造函数。
*
*该类应使用随机类(来自JavaAPI)生成
*数组中每个元素的0到9范围内的随机数。
*/
公共彩票(){
Random rand=新的Random(System.currentTimeMillis());
乐透数字=新整数[5];
for(int i=0;i
将内部for循环从
for(int j=0;j
更改为
for(int j=0;j
,否则您将运行
i++
太多次,并超出
lotteryNumbers
在第二个for循环中的界限,同时计算
i++
。所以在这两个for循环中都计算
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;
                }
            }
        }
    }
if(usersNumbers.length==lotteryNumbers.length){
for(int i=0;i
你在
compareNumbers
方法中的
j
循环中激励了
i
。谢谢你,这起作用了。我不知怎么的,我不确定这是否与我的match++有关,这就是我将其注释掉的原因。不客气。如果我的答案对你有帮助,请点击我答案左侧的“勾号”接受我的答案。这样,其他人会更容易找到答案。当然,我希望我能投赞成票,但我需要15个代表lol,但没有勇气,谢谢你。@13lank\U null这没那么重要。谢谢你的回答。:)