就是将"tobeornottobe"这个串英文逐字颠倒但是最终结果却出现otebrotonot少了"eb"而且后面还有一串错误Exceptioninthread"main"java.lang.StringIndexOutOfBoundsException:Stringind...
就是将"to be or not to be" 这个串英文逐字颠倒
但是最终结果却出现ot eb ro ton ot 少了"eb"而且后面还有一串错误
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at 字符串.t2.main(t2.java:18)
下面是代码。
public class t2 {
public static void main(String[] args) {
String en="to be or not to be";
int sum=0;
String x2=" ";
int i=0;
int i2=i;
int x=en.indexOf(' ');
for(int i4=0;i4>=0;i4++){
if(x!=-1){
x=en.indexOf(' ');
x2=en.substring(0,x);
char[]q2=new char[x2.length()];
for(i=0;i
char q=x2.charAt(i);
q2[i]=q;
if(i==x2.length()-1){
for(i2=q2.length-1;i2>=0;i2--){
System.out.print(q2[i2]);
}//for3
System.out.print(" ");
en=en.substring(x+1);//重置en内容
}//if2
}//for2
}//if 取得一串字母
else{
en=en.substring(x+1);
x2=en;
char[]q2=new char[x2.length()];
for(i=0;i
char q=x2.charAt(i);
q2[i]=q;
if(i==x2.length()-1){
for(i2=q2.length-1;i2>=0;i2--){
System.out.print(q2[i2]);
}
}
}break;
} //else
} //for1
}
}
展开