(其实我不知道如何写这个代码,我查了一下网上找到它也许是这样,但是当我运行它,它没有工作。递归方法接受两个参数:一个字符串S和一个intñ
例如输入(“College”,2),应该输出(“College”,“College”),但是显示无法读取 我只是不知道该如何解决这个问题 请教我如何编写代码
-------编写一个名为printStr的RECURSIVE方法,该方法接受两个参数:一个String和一个int n。该方法应该返回一个包含字符串s的字符串s次,每次用空格隔开,假设n> = 1.
例如,调用printStr(“Lehman”,2)应返回“Lehman Lehman”,并调用printStr(“The Bronx”,4)应返回“The Bronx The Bronx The Bronx The Bronx”。
打电话给班级家庭作业5_2。在主要方法中,多次调用您的方法printStr来测试它。
进口java.util.Scanner的;
公共类Homework5_2 {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int n = 0;
String s = args[1];
System.out.print(printStr(s,n));
}
public static String printStr(String s, int n){
if (n==0) {
return "";
}
return s + printStr(s, n - 1);
}
你在处理什么问题? –
为了让回答者或其他有类似问题的人更容易,请编辑添加一个特定的问题陈述 - “不起作用”可以假定,但* how *不起作用?什么错误信息或不正确的行为是特征? –