- //数字反转与字符串反转
- package cn.itcast.test;
- public class Demo {
- public static void main(String[] args) {
- inverse(102);
- inverse1("abcd");
- inverse2("abcdefg");
- }
- public static void inverse(int n){
- System.out.print(n % 10);
- if (n >= 10)
- inverse(n / 10);
- }
- public static void inverse1(String s1){
- int n=s1.length()-1;
- for(int i=0;i<=n;i++){
- System.out.print(s1.charAt(n-i));
- }
- System.out.println();
- }
- public static void inverse2(String s1){
- StringBuilder sb = new StringBuilder();
- char[] ch=s1.toCharArray();
- int n=s1.length()-1;
- for(int i=0;i<=n;i++){
- sb.append(ch[n-i]);
- }
- System.out.println(sb.toString());
- }
- }
复制代码 |