使用java将一个int数字进行翻转,例如32输出2

public class Test {
       public static int numberReverse(int number) {
              int temp = number;
              String str = "" + temp;
              char[] ch = str.toCharArray();
              int length = str.length();
              int i = 0;
              while(i < length) {
                    int flag = ch[i];
                    ch[i] = ch[length-1];
                    ch[length-1] = (char) flag;
                    ++i;
                    --length;
              }
              length = str.length();
              int count = 0 ;//记录前面有多少个零
              for(i = 0; i < length; i++) {
                   if(ch[i] == '0') {
                         ++count;
                   }
                   if(ch[i] != '0') {
                         break;
                   }
               }
               if(count == 0) {
                    System.out.println(ch);
               } else {
                    if(length==1) {
                        System.out.println(ch);
                      }
                 for(i = count;i < length;i++) {
                    System.out.print(ch[i]);
                 }
              }
              return 0;
              }
        public static void main(String[] args) {
              numberReverse(104);
        }
}


你可能感兴趣的:(java开发)