递归函数中读取字符串的java助手方法

ia2d9nvy  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(315)

我有一个问题,在我已经工作的递归函数中添加一个只使用2个参数的helper方法,当添加第3个(helper方法)时,我的代码中断并寻找解决方案。这个程序使用一个扫描器,键盘输入一个字符串,另一个输入一个字符,然后输出字母出现的次数。错误发生在第二个if语句和两个return语句上。在第二次键盘输入后,我得到一个错误:
线程“main”java.lang.stringindexoutofboundsexception中出现异常:字符串索引超出范围:-1

import java.util.Scanner;

public class recursiveString {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String input = sc.nextLine();
        System.out.println("Enter a character to find number of occurences: ");
        char character = sc.next().charAt(0);
        System.out.println(character + " occurred " + count(input, character, input.length() - 1) + " times.");

    }

    public static int count(String str, char a, int high) {

        if (str.length() == high) // set equal to high to stop the recursion from infinitely looping
            return high;
        if (str.charAt(str.length() - 1) != a) // if the character in the string is  not equal to "a" subtract from count(substring)
            return count(str.substring(0, str.length() - 1), a, high - 1);
        else 
            return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
            // else add +1 to count for each instance of "a" in the string

    }

}
q8l4jmvw

q8l4jmvw1#

您缺少递归方法的设计:首先,您应该关注一个问题,并为基本情况或多个情况定义它。
我对这个问题的看法是,基本情况是空字符串(但在此之前,请确保它不是空字符串) null )或者如果 high 设置为0。
我对 high 您可以使用它来设置要检查字符串中出现的字符的数量 a ; 随着绳子变大,检查起来会更直接,吉文 high 搜索字符出现的意义 a 进入 str.substring(0,high) ,但我试着保持它与您的代码相似。

//we'll use high to "tell" the count method how many characters it will consider into the occurrences from the end of the given string
public static int count(String str, char a, int high) {
    //if the string isn't valid or high just tells it to stop, return 0 as there can be no occurrences of a in str
    if(str == null || str.equals("") || high == 0)
      return 0;

    // if the last character in the string is not equal to a, let's just shrink the string
    if (str.charAt(str.length() - 1) != a)
        return count(str.substring(0, str.length() - 1), a, high - 1);

    // otherwise add this 1 occurrence to the ones it will find in the rest of the string
    else 
        return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
}

电话 main 将是:

System.out.println(character+ " occurred " + count(input, character, input.length()) + " times.");
tf7tbtn2

tf7tbtn22#

下面是一个可能的解决方案,可以帮助您避免索引越界:

public static int count(String str, char a, int high) {

    if (str == null || str.length() == 0) {
    // just to be extra safe, if we have an empty string or null 
        return 0;

    }
    //changed this end condition - now high describes how many steps we take before returning the answer
    if (high == 0) // to stop the recursion from infinitely looping
        return high;
    if (str.charAt(str.length() - 1) != a) // if the last character in the string is not equal to "a" subtract from count(substring)
        return count(str.substring(0, str.length() - 1), a, high - 1);
    else 
        return 1 + count(str.substring(0, str.length() - 1), a, high - 1); 
        // else add +1 to count for each instance of "a" in the string

}

相关问题