将给定的一字符串倒序输出,用 JAVA
7个回答
展开全部
public class Three {
public static void main(String args[]) {
String strA = "123456";
for (int i = 0; i < strA.length(); i++) {
char getStr = strA.charAt(strA.length() - i - 1);
System.out.println(getStr);
}
}
}
运行结果:
6
5
4
3
2
1
public static void main(String args[]) {
String strA = "123456";
for (int i = 0; i < strA.length(); i++) {
char getStr = strA.charAt(strA.length() - i - 1);
System.out.println(getStr);
}
}
}
运行结果:
6
5
4
3
2
1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Three {
public static void main(String args[]) {
String strA = "123456";
Sring strB=''';
for (int i = strA.length(); i>0; i++) {
strB+= strA.subString(i-1,i) }
System.out.print("逆序单词"+strB);
}
public static void main(String args[]) {
String strA = "123456";
Sring strB=''';
for (int i = strA.length(); i>0; i++) {
strB+= strA.subString(i-1,i) }
System.out.print("逆序单词"+strB);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Three {
public static void main(String args[]) {
String str = "abcdefg";
for (int i = str.length()-1; i >=0; i--) {
System.out.println(str.substr(i,i+1));
}
}
}
public static void main(String args[]) {
String str = "abcdefg";
for (int i = str.length()-1; i >=0; i--) {
System.out.println(str.substr(i,i+1));
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一、利用String类的toCharArray(),再倒序输出数组。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void main (String args[]){
String originalString;
String resultString = "";
originalString = JOptionPane.showInputDialog("Please input a String: ");
char[] charArray = originalString.toCharArray();
for (int i=charArray.length-1; i>=0; i--){
resultString += charArray[i];
}
JOptionPane.showMessageDialog(null, resultString, "Reverse String", JOptionPane.INFORMATION_MESSAGE);
}
}
二、利用String类提供的subString()方法,利用递归的办法输出倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
if (str.length() == 1){
System.out.print(str);
}else{
String subString1 = str.substring(0, str.length()-1);
String subString2 = str.substring(str.length()-1);
System.out.print(subString2);
reverseString (subString1);
}
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog("Please input a String: ");
reverseString (originalString);
}
}
三、把字符串定义成StringBuffer类,用StringBuffer类中的reverse()方法直接倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
StringBuffer stringBuffer = new StringBuffer (str);
System.out.print(stringBuffer.reverse());
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog("Please input a String: ");
reverseString (originalString);
}
}
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void main (String args[]){
String originalString;
String resultString = "";
originalString = JOptionPane.showInputDialog("Please input a String: ");
char[] charArray = originalString.toCharArray();
for (int i=charArray.length-1; i>=0; i--){
resultString += charArray[i];
}
JOptionPane.showMessageDialog(null, resultString, "Reverse String", JOptionPane.INFORMATION_MESSAGE);
}
}
二、利用String类提供的subString()方法,利用递归的办法输出倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
if (str.length() == 1){
System.out.print(str);
}else{
String subString1 = str.substring(0, str.length()-1);
String subString2 = str.substring(str.length()-1);
System.out.print(subString2);
reverseString (subString1);
}
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog("Please input a String: ");
reverseString (originalString);
}
}
三、把字符串定义成StringBuffer类,用StringBuffer类中的reverse()方法直接倒序字符串。
示例代码:
import javax.swing.JOptionPane;
public class ReverseString {
public static void reverseString (String str){
StringBuffer stringBuffer = new StringBuffer (str);
System.out.print(stringBuffer.reverse());
}
public static void main (String args[]){
String originalString;
originalString = JOptionPane.showInputDialog("Please input a String: ");
reverseString (originalString);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//ep.1
String strrev="reverse";
ArrayList<String> alrev=new ArrayList<String>();
for(int i=0;i<strrev.length();i++){
alrev.add(String.valueOf(strrev.charAt(i)));
}
Collections.reverse(alrev);
System.out.println(alrev);
//ep.2
StringBuffer strbrev=new StringBuffer("reverse");
strbrev=strbrev.reverse();
System.out.println(strbrev);
String strrev="reverse";
ArrayList<String> alrev=new ArrayList<String>();
for(int i=0;i<strrev.length();i++){
alrev.add(String.valueOf(strrev.charAt(i)));
}
Collections.reverse(alrev);
System.out.println(alrev);
//ep.2
StringBuffer strbrev=new StringBuffer("reverse");
strbrev=strbrev.reverse();
System.out.println(strbrev);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询