我写了一个更通用的方法,不限于3个字符的,任意长度的均可,权当交流:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = input.next();
Reverse(str);
}
public static void Reverse(String str){
System.out.println("反转后的字符串为:");
for(int i = 0; i String temp = "";
temp = temp + str.charAt(str.length()-i-1);
System.out.print(temp);
}
不知为何你有这种想法,如果有好的算法可以和我讨论下,下面我写了2个代码希望能帮到你。
package app;
public class TransDemo {
//abcdef“反转后为“fedcba
public static void main(String[] args){
String str = "abcdef";
char[] ary = str.toCharArray();
for(int i = 0; i
int temp = ary[ary.length-i-1]-ary[i];
ary[i] += temp;
ary[ary.length-i-1] -=temp;
}
str = new String(ary);
System.out.println(str);
}
}
package app;
public class TransDemo {
//abcdef“反转后为“fedcba
public static void main(String[] args){
String str = "abcdef";
char[] ary = str.toCharArray();
for(int i = 0; i
char c = ary[i];
ary[i] = ary[str.length()-1-i];
ary[str.length()-1-i] = c;
}
str = new String(ary);
System.out.println(str);
}
}
以上是我如何在Java中读取输入的字符串?的详细内容。更多信息请关注PHP中文网其他相关文章!