为什么程序不继续

vip*_*lnj 0 java recursion command-line-arguments

以下程序是一个递归程序,用于检查数组中的重复条目.程序编译没有错误,但是在我输入命令行参数并按Enter后,它不会继续.光标只是闪烁!它也不会返回任何运行时错误!如果有人解释为什么会这样,那将非常有帮助!谢谢!:)

import java.io.*;
    class RepeatEntries_Recursive
    {
        static int i=0,flag=0;
        public static void main(String[] args) throws IOException
        {
            int[] inp = new int[6];
            for(int k=0;k<args.length;k++)
                inp[k] = Integer.parseInt(args[k]);
            boolean hasItRepeated = Repeating(inp,i);
            if(hasItRepeated == true)
                System.out.println("\nYes, there are entries that repeat in the array!");
            else
                System.out.println("\nNo, entries don't repeat in the array");
        }
        static boolean Repeating(int[] inp,int i)
        {
            for(int j=0;j<inp.length;j++)
            {
                if(inp[i] == inp[j])
                    flag = 1;
                while(i<inp.length-1)
                    Repeating(inp,i+1);
            }
            if(flag==1)
                return true;
            else
                return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Daw*_*ica 6

while(i<inp.length-1)
    Repeating(inp,i+1);
Run Code Online (Sandbox Code Playgroud)

你的程序无法逃脱这个循环.

  • 在调试器中逐步执行它,看看`while`是否真的有意义.你可能想要`if`所以它在每个级别都会递归一次,而不是在每个级别都是无限的. (2认同)