我制作了一个程序来反转输入字符串的单词有一个错误代码:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range: 17 at java.lang.String.charAt(String.java:695) at Rev.main(Rev.java:14)
主程序代码为:
import java.io.*; class Rev { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter String"); String a=br.readLine(); a=" "+a+" "; int x=a.length(); String b; for(int z=a.length()-1;z>=0;z--) { if (a.charAt(z)==' ') { b=a.substring(z,x); System.out.print(b+" "); a=a.replace(b,""); } } }
}