for循环到递归JAVA

for循环到递归JAVA,java,recursion,Java,Recursion,我需要写一个递归函数来发现给定长度的密码,允许的字符是a-z。我不允许使用循环,但我不能让它工作 这里是我的解决方案,其中有一个for循环,我需要执行一些操作 public static String findPassword(String p, int length) { String pass = ""; return findPassword(p, length, pass); } private static String findPassword(String p,

我需要写一个递归函数来发现给定长度的密码,允许的字符是a-z。我不允许使用循环,但我不能让它工作

这里是我的解决方案,其中有一个for循环,我需要执行一些操作

public static String findPassword(String p, int length) {
    String pass = "";
    return findPassword(p, length, pass);
}

private static String findPassword(String p, int length, String pass) {

    String abc = "abcdefghijklmnopqrstuvwxryz";

    if (pass.length() == length) {
        if (p.equals(pass))
            return pass;
        return "";
    }

    for (int i = 0; i < abc.length(); i++) {

        if (p.equals(findPassword(p, length, pass + abc.charAt(i))))
            return findPassword(p, length, pass + abc.charAt(i));
    }

    return "";
}
公共静态字符串findPassword(字符串p,int-length){
字符串pass=“”;
返回findPassword(p、长度、过程);
}
私有静态字符串findPassword(字符串p、整数长度、字符串传递){
字符串abc=“abcdefghijklmnopqrstuvxryz”;
if(pass.length()=长度){
如果(p等于(通过))
回程通行证;
返回“”;
}
对于(int i=0;i

我尝试过我能想到的任何事情,但都没有效果。

您可以尝试将密码转换为一个数字,然后使用递归增加p的值,直到转换后的密码值:

public class Password {
    public static void main(String[] args) {
        System.out.println(findPassword(0, "this is the password to find"));
    }

    public static String findPassword(int p, final String passwordToFind) {
        System.out.println(p); //just checkin it's working
        if (p == passToInt(passwordToFind))
            return passwordToFind;
        else
            return findPassword(++p, passwordToFind);
    }

    private static int passToInt(String passwordToFind) {

        int a = 0;
        for (byte b : passwordToFind.getBytes()) {
            a += b;
        }
        return a;
    }
}
这不是世界上最漂亮的代码,我也没有深入研究它是否真的有效,只是想让你知道如何不使用循环来解决它,而只是一个简单的递归。

publicstaticvoidmain(String[]args)
public static void main(String[] args)
{
    System.out.println(findPassword("stack", 5));
}

private static final String alphabet = "abcdefghijklmnopqrstuvwxyz";

public static String findPassword(String solution, int length)
{
    return findPassword(solution, length, "", 0);
}

private static String findPassword(String solution, int length, String pass, int alphabetIndex)
{
    if (pass.length() == length)
    {
        if (solution.equals(pass))
            return pass;
        return "";
    }

    if (alphabetIndex < alphabet.length())
    {
        String found = findPassword(solution, length, pass + alphabet.charAt(alphabetIndex), 0);

        if (found.length() == 0)
        {
            return findPassword(solution, length, pass, alphabetIndex + 1);
        }

        return found;
    }

    return "";
}
{ System.out.println(findPassword(“stack”,5)); } 私有静态最终字符串字母表=“abcdefghijklmnopqrstuvwxyz”; 公共静态字符串findPassword(字符串解决方案,整数长度) { 返回findPassword(解决方案,长度,“”,0); } 私有静态字符串findPassword(字符串解决方案、整数长度、字符串传递、整数字母索引) { if(pass.length()=长度) { if(解等于(通过)) 回程通行证; 返回“”; } 如果(字母索引<字母表长度()) { 找到的字符串=findPassword(解决方案、长度、过程+字母表字符(字母索引),0); if(find.length()==0) { 返回findPassword(解决方案、长度、过程、字母索引+1); } 发现退货; } 返回“”; }

**您的abc字符串在字符“y”后面有多余的“r”。

“…发现给定长度的密码…”这是什么意思?
p
参数是什么?我不知道你想做什么,你叫什么来查找密码?似乎
p
是你试图复制的字符串(因为当你找到一个相等的字符串时返回)。但是您可以返回
p
新字符串(p)
。我不明白这个程序的意思。我认为“pass”是一个变量,就像在一种尾部递归中使用的一样。p是密码。我需要找到她,唯一允许我使用p的方法是在equals上。@IdoSamselik-不客气。如果你能投票支持我,我将不胜感激。