我正在研究一个递归方法,它将返回并在我的主方法中打印一个字符串,它将向下计数1表示N。为字符串的每个递归删除一个字母。直到N变成1或者字符串有一个字母。(N为命令行上的int )
例如,如果我的命令行的薪酬是:5瓦伦丁
它还应产出:
5瓦伦丁,4松节油,3牙本质,2齿,1齿,
到目前为止,我成功地计算了命令行参数输入的数字。我只是不知道如何从字符串中删除一个字母?:o
到目前为止我的代码是:
public static void main(String[] args){
int number = Integer.parseInt(args[0]);
String word = new String("");
word = args[1];
String method = recursive.method1(number);
System.out.println(method);
}
public static String method1(int number){
if (number < 0){
return "";
}
else{
return number + ", " + method1(number - 1);
}
}发布于 2014-02-08 09:30:48
您可以阅读subString()文档,了解如何获取字符串的各个部分。
wordword添加到return语句中:return number + " " + word +...1st索引调用原始单词的子字符串<=0而不是<0代码:
public static void main(String[] args){
int number = 5;
String word = new String("");
word = "Valentine";
String method = Recurcive.method1(number, word);
System.out.println(method);
}
public static String method1(int number, String word){
if (number <= 0){
return "";
}
else{
return number + " " + word + ", " + method1(number - 1, word.substring(1));
}
}给予,
5 Valentine, 4 alentine, 3 lentine, 2 entine, 1 ntine, https://stackoverflow.com/questions/21644186
复制相似问题