有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

数组Java彩票类

因此,我正在编写一个Lottery类和一个演示Lottery类的类。我已经完成了工作并编译了它,但当我运行它时

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4  
at LotteryDemo.main(LotteryDemo.java:21)

这是我的代码,任何帮助都将不胜感激

类:

    import java.util.Random;


import java.util.Scanner;

   public class Lottery
   {


  int NUM_DIGITS = 5;   
  int[] lotteryNumbers = new int[4];




  public Lottery(){
     Random rand = new Random();
     lotteryNumbers[0] = rand.nextInt(10);
     lotteryNumbers[1] = rand.nextInt(10);
     lotteryNumbers[2] = rand.nextInt(10);
     lotteryNumbers[3] = rand.nextInt(10);
     lotteryNumbers[4] = rand.nextInt(10);
     getLotteryNums();

  }


  public int numIntsInCommon(int[] picks){
     int inCommon = 0;

     for (int counter = 0; counter < 5; counter++)
     {
        for (int index = 0; index < 5; index++)
        {
           if (lotteryNumbers[counter] == picks[index])
              inCommon++;
           return inCommon;
        }
        return inCommon;
     }

     return inCommon;
  }

  public String getLotteryNums(){
     return lotteryNumbers[0] + ", " + lotteryNumbers[1] + ", " + 
                    lotteryNumbers[2] + ", " + lotteryNumbers[3] + ", " +
                    lotteryNumbers[4];
  }




}

The LotteryDemo类:

    import java.util.*;


   public class LotteryDemo
   {
  public static void main(String[] args)
  {

        Scanner input = new Scanner(System.in);
     int[] userNumbers = new int[4];

     System.out.println("Please supply your lottery picks. Choose unique numbers between 0 and 9.");
     System.out.print("Enter digit 1: ");
        userNumbers[0] = input.nextInt();
        System.out.print("Enter digit 2: ");
        userNumbers[1] = input.nextInt();
        System.out.print("Enter digit 3: ");
        userNumbers[2] = input.nextInt();
        System.out.print("Enter digit 4: ");
        userNumbers[3] = input.nextInt();
        System.out.print("Enter digit 5: ");
        userNumbers[4] = input.nextInt();


    Lottery lottery = new Lottery();


    System.out.print("Lottery Numbers: " + lottery.getLotteryNums());
  System.out.print("Number of matching digits: " + lottery.numIntsInCommon(userNumbers));
    if(lottery.numIntsInCommon(userNumbers) == 5)
        System.out.print("All 5 match! You have hit the jackpot!");
  }


}

共 (3) 个答案

  1. # 1 楼答案

    int[] lotteryNumbers = new int[4];
    

    这意味着数组有4个元素或lotteryNumbers[0]lotteryNumbers[3]

    因此lotteryNumbers[4] = rand.nextInt(10);将导致上述异常

    userNumbers在main中类似,这导致了异常

    int[] userNumbers = new int[4];
    ...
    userNumbers[4] = input.nextInt(); // java.lang.ArrayIndexOutOfBoundsException
    
  2. # 2 楼答案

    new int[4]分配数组,这意味着它有4个编号为0到3的元素。 当您访问索引为4的元素时,它超出了范围

  3. # 3 楼答案

    Java中的数组具有如下实例化:

    type[] name = new type[size]
                           ^
    

    大小表示数组中索引数。如您所知,数组从零开始,因此:

    int[] userNumbers = new int[4]
    

    生成一个数组:

    [ ][ ][ ][ ]
     0  1  2  3
    

    因此userNumbers[4]不存在。您需要为五个索引创建userNumber[5]