用java编写程序,使一段字符串输出时,与原串字符顺序相反

2024-12-30 14:14:48
推荐回答(3个)
回答1:

  • 如果是字符串的话,可以通过以下方式逆序输出:

public class reverseTest {
public static void main(String[] args) {
String originalString = "abcdefg";
StringBuffer stringBuffer = new StringBuffer(originalString);
System.out.println(stringBuffer.reverse());
}
}

  • 字符串的话,还可以通过以下方式逆序输出:

public class javatest {
  public static void main(String[] args) {
   String originalString = "abcdefg";
String resultString = "";
char[] charArray = originalString.toCharArray();
for (int i = charArray.length-1; i>=0; i--){
resultString +=charArray[i];
}
System.out.println(resultString);
}
}

回答2:

String str="ddddffffffggggg";
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());

回答3:

import java.util.*;
public class TestTemp {

public static void main(String[] args) {
String str = "ABC";
StringBuffer sbr = new StringBuffer();
for(int i=str.length()-1;i>=0;i--){
char c = str.charAt(i);
sbr.append(c);

}
System.out.println("result is :"+sbr);

}

}

慢了一步!