//method I'm trying to call
static String sentenceReversal(String input) {
int x = input.indexOf(" ");
//Base Case
if(x == -1)
return input;
//Recursive Method
return sentenceReversal(input.substring(x+1)) +" "+ input.substring(0, x);
case 6:
System.out.println("Enter a string to reverse:");
String input6 = scan.next();
System.out.println(sentenceReversal(input6));
break;
将该方法调用为sentenceReversal("Hello World")
返回World Hello
,但用input6
调用它会返回Hello
,其他什么都不是,有什么 idea 吗?
我也专门为这种情况做了一台 scanner ,因为我之前得到的是InputMismatchException
分
同样,当在切换后输入数字后,字符串的下一部分会出现.