需求:定义一个方法,实现字符串反转
import java.util.Scanner;
public class StringDemo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println("请输入一个字符串");
System.out.println(strReverse(str));
}
static String strReverse(String s) {
String newStr = "";
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
newStr = newStr + c;
}
return newStr;
}
}