此代码有效,但它不断给我数组越界异常。即使我将数组大小更改为 6 并在最后留下 2 个空槽,它也会引发异常。有人可以找出问题吗?
int [] arrayCMYK = new int [4];
getCMYK(arrayCMYK);
static int getCMYK (int arrayCMYK[])
{
Scanner input = new Scanner(System.in);
//C
System.out.println("\n\nPlease Enter the 'C' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[0] = input.nextInt();
while(arrayCMYK [0] > 100 || arrayCMYK [0] < 0 )
{
System.out.println("\n\nError\nPlease Enter the 'C' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[0] = input.nextInt();
}
//M
System.out.println("\n\nPlease Enter the 'M' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[1] = input.nextInt();
while(arrayCMYK [1] > 100 || arrayCMYK [1] < 0 )
{
System.out.println("\n\nError\nPlease Enter the 'M' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[1] = input.nextInt();
}
//Y
System.out.println("\n\nPlease Enter the 'Y' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[2] = input.nextInt();
while(arrayCMYK [2] > 100 || arrayCMYK [2] < 0 )
{
System.out.println("\n\nError\nPlease Enter the 'Y' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[2] = input.nextInt();
}
// K
System.out.println("\n\nPlease Enter the 'K' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[3] = input.nextInt();
while(arrayCMYK [3] > 100 || arrayCMYK [3] < 0 )
{
System.out.println("\n\nError\nPlease Enter the 'K' value of the CMYK number.");
System.out.println("Press Enter after the number has been entered.");
arrayCMYK[3] = input.nextInt();
}
return arrayCMYK[4];