我寫了一個更通用的方法,不限於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中文網其他相關文章!