Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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 为什么打印为';运行时错误信号-1';函数findFirstZero何时返回-1?_Java_Function_Return_Runtime - Fatal编程技术网

Java 为什么打印为';运行时错误信号-1';函数findFirstZero何时返回-1?

Java 为什么打印为';运行时错误信号-1';函数findFirstZero何时返回-1?,java,function,return,runtime,Java,Function,Return,Runtime,当我通过标准输入给出以下输入时: 四, 1111000 它抛出“运行时错误信号-1”,而不是打印“无零” 为什么会这样? 我使用了在线Ideone编译器来运行代码 class FindZeroCount { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int size=sc.nextIn

当我通过标准输入给出以下输入时:

四, 1111000

它抛出“运行时错误信号-1”,而不是打印“无零”

为什么会这样? 我使用了在线Ideone编译器来运行代码

class FindZeroCount
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int size=sc.nextInt();
        //sc.nextLine();
        int input[]=new int[size];

        for(int i=0;i<size;i++)
            if(sc.hasNextInt())
                input[i]=sc.nextInt();
            else
                System.out.println("Not enough inputs given");

        int zi=findFirstZero(input,0,size-1);
        if(zi==-1)
            System.out.println("No zeros");
        System.out.println("Number of zeros = "+ (size-zi));

    }
    public static int findFirstZero(int input[],int l,int h)
    {
        int mid;
        if(h>=l)
        {
            mid=l + (h-l)/2;
            if((mid==0 || input[mid-1]==1) && input[mid]==0)
                return mid;
            else if(input[mid]==1 && input[mid+1]==0)
                return mid+1;
            else if(input[mid]==1)
                return findFirstZero(input,mid+1,h);
            else
                return findFirstZero(input,l,mid-1);

        }
        return -1;

    }
}
类FindZeroCount
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
扫描仪sc=新的扫描仪(System.in);
int size=sc.nextInt();
//sc.nextLine();
int input[]=新的int[size];
对于(int i=0;i=l)
{
mid=l+(h-l)/2;
如果((mid==0 | |输入[mid-1]==1)和输入[mid]==0)
中途返回;
else if(输入[mid]==1&&input[mid+1]==0)
返回mid+1;
else if(输入[mid]==1)
返回findFirstZero(输入,mid+1,h);
其他的
返回findFirstZero(输入,l,mid-1);
}
返回-1;
}
}

当使用问题中所述的输入运行代码时,您会得到一个
数组索引OutofBoundsException
:在第三次递归调用中,您有
l=3
h=3
,因此
mid=3
。当代码进入第二条if语句(
else if(input[mid]==1&&input[mid+1]==0)
)时,您试图访问索引号4,但数组的索引仅从0到3

尝试使用调试器单步执行代码:在
intzi=findFirstZero(input,0,size-1)行中放置一个断点并逐行浏览


但是,我无法输入您在上面给出的整个数字序列,因为您的第一个数字(即4)告诉我您可以输入多少个数字。当我输入4作为第一个数字时,代码将在我输入第四个1后开始计算给定的序列。

是否尝试通过调试器一步一步地运行代码?您发现了什么?当我运行您的程序并粘贴input 4 1 1 0 0 0 0时,它给了我java.lang.ArrayIndexOutOfBoundsException。顺便问一下,你想实现什么?我想计算一个数组中的零数,该数组只包含按降序排序的1和0。。我将第三个条件修改为“if(input[mid]==1&&(mid==input.length-1 | | input[mid+1]==0)),现在它可以正常工作了……非常感谢。。我更改了这样的条件“if(input[mid]==1&&(mid==input.length-1 | | input[mid+1]==0)),现在可以正常工作了。。。