can';t执行数组验证(越界)-java

can';t执行数组验证(越界)-java,java,arrays,Java,Arrays,我不太清楚这里发生了什么 代码应该根据固定ID号数组验证输入。但每次我故意输入错误的数字时,它都会说“数组超出范围” 不太确定是什么导致了问题,也许有人能指出我的错误 import java.util.Scanner; public class isValid { static int accNum[] = {11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 10101, 20202, 30303, 40404

我不太清楚这里发生了什么

代码应该根据固定ID号数组验证输入。但每次我故意输入错误的数字时,它都会说“数组超出范围”

不太确定是什么导致了问题,也许有人能指出我的错误

import java.util.Scanner;
 public class isValid
{
    static int accNum[] = {11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 10101, 20202, 30303, 40404, 50505, 60606, 70707, 80808, 90909};

public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    int conti = -99;
    int search = 0;
    do
    {
        System.out.print("Enter 5-digit account number you want to validate: ");

        search = keyboard.nextInt();

        sequentialSearch(accNum, search);
        System.out.println("");
        System.out.print("Enter -9 to exit program, or any other number to validate another ID: ");
        conti = keyboard.nextInt();

    } while (conti != -9);
}

public static void sequentialSearch(int[] array,int value)
{
    int index = 0;        
    int element = -1;      
    boolean found = false;    

    while (!found && index < array.length)
    {
        if (array[index] == value)
        {
            found = true;
            element = index;
            break; //prevent index addition if value found
        }
        index++;
    }

    if (array[index] == value)
    {
        System.out.println("Account " + value + " is valid.");
    }
    else 
    {
        System.out.println("Account " + value + " is invalid.");
    }
}
}
import java.util.Scanner;
公共类是有效的
{
静态int accNum[]={11111222233333,4444445555555,66666,77777,88888,99999,10101,20202,30303,40404,50505,60606,70707,80808,909};
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
int conti=-99;
int搜索=0;
做
{
System.out.print(“输入要验证的5位数帐号:”);
search=keyboard.nextInt();
顺序搜索(accNum,search);
System.out.println(“”);
System.out.print(“输入-9退出程序,或输入任何其他数字验证另一个ID:”);
conti=keyboard.nextInt();
}而(conti!=-9);
}
公共静态void sequentialSearch(int[]数组,int值)
{
int指数=0;
int元素=-1;
布尔值=false;
而(!found&&index
问题如下:
错误消息:

我修改了代码,应该是这样的:

    import java.util.Scanner;
    public class isValid
    {
        static int accNum[] = {11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 10101, 20202, 30303, 40404, 50505, 60606, 70707, 80808, 90909};

        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);

            int conti = -99;
            int search = 0;
            do
            {
                System.out.print("Enter 5-digit account number you want to validate: ");

                search = keyboard.nextInt();

                sequentialSearch(accNum, search);
                System.out.println("");
                System.out.print("Enter -9 to exit program, or any other number to validate another ID: ");
                conti = keyboard.nextInt();

        } while (conti != -9);
    }

    public static void sequentialSearch(int[] array,int value)
    {
        int index = 0;        
        int element = -1;      
        boolean found = false;    

        while (!found && index < array.length)
        {
            if (array[index] == value)
            {
                found = true;
                element = index;
                break; //prevent index addition if value found
            }
            index++;
        }

        if (found)
        {
            System.out.println("Account " + value + " is valid.");
        }
        else 
        {
            System.out.println("Account " + value + " is invalid.");
        }
    }
  }
import java.util.Scanner;
公共类是有效的
{
静态int accNum[]={11111222233333,4444445555555,66666,77777,88888,99999,10101,20202,30303,40404,50505,60606,70707,80808,909};
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
int conti=-99;
int搜索=0;
做
{
System.out.print(“输入要验证的5位数帐号:”);
search=keyboard.nextInt();
顺序搜索(accNum,search);
System.out.println(“”);
System.out.print(“输入-9退出程序,或输入任何其他数字验证另一个ID:”);
conti=keyboard.nextInt();
}而(conti!=-9);
}
公共静态void sequentialSearch(int[]数组,int值)
{
int指数=0;
int元素=-1;
布尔值=false;
而(!found&&index
注意顺序搜索函数的while循环之后if条件的变化


为什么呢?由于索引等于数组索引之外的a值,以防数组中不存在该值。

好的,很抱歉我以前的解决方案有点愚蠢,但现在我可以建议您的第二种解决方案是这样做

while (!found && index < accNum.length)
{
    if (accNum[index] == value)
    {
        found = true;
        element = index;
        System.out.println("Account " + value + " is valid.");
    }
    else
    {
        System.out.println("Account " + value + " is invalid.");
        break;
    }
    index++;
}
while(!found&&index

描述:-您的值将被匹配一次,并打印
有效的
消息。如果它与值不匹配,那么它将打印无效消息,循环将中断

首先,为什么在sequentialSearch函数中直接使用accNum,而显然它也是作为参数数组传递的呢??这与问题无关,但需要重新审查。@UsamaZafar-好的,我已经解决了这个问题。请看答案部分,您会在那里找到非常详细的信息。@UsamaZafar我真的很抱歉我之前的错误。回答。它有点傻,我只用一种方式检查它。但没有查看值无效时会发生什么。非常感谢你的建议,鼓励我仔细研究。我应该从一开始就使用布尔值来确定if-else。谢谢