我在使用2个参数时在我已经工作的递归函数中添加辅助方法时遇到问题,当添加第3个(帮助方法)时,我的代码中断并寻找解决方案。该程序使用扫描仪键盘输入字符串,另一个字符输入,然后输出字母的出现次数。第二个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
}
}
答案 0 :(得分:2)
你错过了递归方法的设计:首先,你应该关注一个问题并为基本案例定义它,或者如果有多个案例则定义它。
我对这个问题的看法是基本情况是空字符串(但在此之前,确保它不是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.");
中的来电将是:
std::string line;
std::vector<std::string> lines_with_data;
while(std::getline(f,line)) {
// check if line is empty or contains only whitespace
if(!(line.empty() ||
(std::find_if_not(line.begin(),line.end(),std::isspace) != line.end())) {
lines_with_data.push_back(line);
}
}
if(lines_with_data.empty()) { // No data could be found
return error("Empty file");
}
答案 1 :(得分:1)
这是一个可能的解决方案,可以帮助您避免索引超出范围:
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
}