Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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_Indexoutofboundsexception - Fatal编程技术网

数组越界异常错误java

数组越界异常错误java,java,indexoutofboundsexception,Java,Indexoutofboundsexception,所以我是java的初学者。我试图创建一个程序,告诉我数组中的偶数整数。但是,我不断得到数组越界错误:第29行为10(其中我是sy“公式[偶[b])。请帮助 public class arrayone { public static void main(String args []) { /* tell me the number of even ints in the given array. Note: the % "mod" operator com

所以我是java的初学者。我试图创建一个程序,告诉我数组中的偶数整数。但是,我不断得到数组越界错误:第29行为10(其中我是sy“公式[偶[b])。请帮助

public class arrayone
{
    public static void main(String args [])
    {
        /* 
    tell me the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
    */

    //declare an array
    int[] even= new int[10];

    //int b is for the forloop so that every number can be added
    int b;

    //

    //initialize the scanner, and enter prompt
    Scanner input= new Scanner(System.in);

    //enter prompt
    System.out.printf("Enter 10 random numbers\n");

    //make a forloop so every number they put in will be added to the array
    for(b=0;b<10;b++)
    {
    even[b]=input.nextInt();
    }

//then run the formula
formula(even[b]);
    }

    public static void formula(int a)
    {


        //use an if statement to see if the numbers in the array are odd or even.
        for(a=0;a<=10;a++)
        {
        if((a%2)==0)
        {
            System.out.printf("This number is even\n");
        }
        else
        {
            System.out.printf("This number isn't even\n");

        }
        }
    }
}
公共类数组
{
公共静态void main(字符串参数[])
{
/* 
告诉我给定数组中偶数整数的数目。注意:%的“mod”运算符计算余数,例如5%2是1。
*/
//声明数组
int[]偶数=新的int[10];
//int b表示forloop,因此每个数字都可以相加
int b;
//
//初始化扫描仪,然后输入prompt
扫描仪输入=新扫描仪(System.in);
//输入提示
System.out.printf(“输入10个随机数\n”);
//制作一个forloop,这样他们输入的每个数字都将添加到数组中

对于(b=0;b
对于(b=0;b好的,让我们从头开始

您读取了一组输入并将其存储在一个数组中,目前为止效果良好

然后通过执行
formula(甚至[b])
,您试图只向函数
formula
发送一个值。然而,正如@Sanjeev所指出的,此时您的
b=10
,因为前面的
for
循环,因此给您的
数组超出了范围

然后在
formula
中,你只需要一个
int
,但是你需要这个
int
a
,在
for
循环中重新分配它,顺便说一句,这个循环检查0和10(包括)之间的数字是偶数。我想这不是你想要的

您真正想做的是:

for(int i = 0; i < 10; i++) {
    formula(even[i]);
} 

public static void formula(int a)
{
    if((a%2)==0)
    {
        System.out.printf("This number is even\n");
    }
    else
    {
        System.out.printf("This number isn't even\n");
    }
}
for(int i=0;i<10;i++){
公式(偶数[i]);
} 
公共静态无效公式(int a)
{
如果((a%2)=0)
{
System.out.printf(“此数字为偶数\n”);
}
其他的
{
System.out.printf(“此数字不是偶数\n”);
}
}

公式(偶数);
公共静态无效公式(int[]a)
{
对于(int i=0;i
公式
中根本不应该有
for
循环。你只需要一个
int
我真的对你认为你在做的事情感到困惑。你的
公式
毫无意义,而不是将每个整数都发送到公式为什么不这样发送整个数组?我之后你是如何回答这个问题的t被标记为重复?我想可能我在标记答案时已经在处理它了
for(int i = 0; i < 10; i++) {
    formula(even[i]);
} 

public static void formula(int a)
{
    if((a%2)==0)
    {
        System.out.printf("This number is even\n");
    }
    else
    {
        System.out.printf("This number isn't even\n");
    }
}
formula(even);

public static void formula(int[] a)
{
    for(int i = 0; i < a.length(); i++) {
        if((a[i]%2)==0)
        {
            System.out.printf("This number is even\n");
        }
        else
        {
            System.out.printf("This number isn't even\n");
        }
    }   
}