题目:
输入一个正整数,将其数字逆转。如:12345 处理后 54321
这个题很简单,但是我做不出一个通用公式(如3位数,5位数,7位数的方法不太一样),求高手指点逻辑思维。如果固定三位数,我就是用数组,提取余数,然后颠倒排列输出……求如何在不确定输入几位数的情况下,顺利逆转数字?

解决方案 »

  1.   


    package org.cai.bean;
    public class Test {
    public static int reverseInt(int value){

    String tmp = String.valueOf(value) ;
    StringBuffer sb = new StringBuffer(tmp) ;

    tmp = sb.reverse().toString() ;

    return Integer.valueOf(tmp) ;

    }
    public static void main(String[] args) { System.out.println("123 -->" + Test.reverseInt(123)) ;
    System.out.println("4560 -->" + Test.reverseInt(4560)) ; }}123 -->321
    4560 -->654
      

  2.   

    package org.cai.bean;
    public class Test {
    public static int reverseInt(int value){

    String tmp = String.valueOf(value) ;
    char[] arr = tmp.toCharArray() ;
    char ch ;
    for (int i = 0; i < arr.length/2; i++){
    ch = arr[i] ;
    arr[i] = arr[arr.length-1-i] ;
    arr[arr.length-1-i] = ch ;

    }

    tmp = new String(arr) ;

    return Integer.valueOf(tmp) ;

    }
    public static void main(String[] args) { System.out.println("123 -->" + Test.reverseInt(123)) ;
    System.out.println("4560 -->" + Test.reverseInt(4560)) ; }}
    结果一样啦
      

  3.   

    加个注释行吗?我才学JAVA一个多月,天,看不懂
      

  4.   

    我也写了一个,楼主试试.import java.util.Scanner;
    public class CsdnReverseInt
    {
      public static void main(String[] args)
        {
    Scanner scan=new Scanner(System.in);
    System.out.println("请输入一个整数:");
    int toBeChanged= scan.nextInt();
    String temp=String.valueOf(toBeChanged); //先变成一个字符串。
    int len=temp.length(); //得到数字的位数。
    int result=0; //保存翻转后的结果。
    for(int i=0;i<len;i++) //循环一位一位计算。
    {
      result=result*10+(toBeChanged%10);         //这句要仔细理解。比如123,
      toBeChanged=toBeChanged/10; //第一次计算是resulut=0*10+3=3;
    }         //地2次就是resulut=3*10+2=32;
             //第3次是resulut=32*10+1=321;
    System.out.println("数字 "+temp+" 被翻转后是:  "+result); //输出。
      }
    }
      

  5.   


    这个方法好        tmp = sb.reverse().toString() ;//调用方法  把字符翻转 
      

  6.   

    StringBuffer  --  reverse,觉得这个比较好,将操作交给库函数完成如果需要使用自己的方法解决,toCharArray的方式,挺好。
      

  7.   

    String得到length或者转化数组之后得到size,长度就确定了
      

  8.   


    public class ReversalStr {


    //temp  传0
    public static String reversal(String str , int temp){
    if(temp == str.length() / 2)
    return str ;
    int last = temp == 0 ? str.length() -1 : str.length() - temp - 1;
    return reversal(str.substring(0, temp) + str.charAt(last) + str.substring(temp + 1 , last) + str.charAt(temp) + str.substring(last + 1, str.length()),++temp);
    } public static void main(String[] args) {
    String str1 = "123456";
    String str2 = "123456789";
    System.out.println(reversal(str1 , 0 ));
    System.out.println(reversal(str2 , 0 ));
    }}Console  654321
      987654321
      

  9.   

    2楼给出的代码,就是我1楼所说的思路,即便不注释也应该很容易看懂把2楼的代码高度抽象下(用对象操作模式):public static int reverseInt(int value){
      return Integer.valueOf(new StringBuffer().append(value).reverse().toString());
    }
      

  10.   


    package org.cai.bean;
    public class Test {
        public static int reverseInt(int value){
            
            String tmp = String.valueOf(value) ;//整数换成为字符串
            StringBuffer sb = new StringBuffer(tmp) ;//构建StringBuffer因为StringBuffer有互换元素位置的方法
            
            tmp = sb.reverse().toString() ;//互换元素位置
            
            return Integer.valueOf(tmp) ;
            
        }
        public static void main(String[] args) {        System.out.println("123 -->" + Test.reverseInt(123)) ;//不解释
            System.out.println("4560 -->" + Test.reverseInt(4560)) ;    }}
      

  11.   

    自己做了一个二楼的…
    Quote:

    Scanner in=new Scanner(System.in);

    System.out.println("输入一个数字:");
    int number=in.nextInt();
    int temp=number; //保存输入数字
    int count=1;
    for(int i=0;i<50;i++){ //提取位数,也就是数组长度
    if(number/10!=0){
    count++; //位数
    number/=10;
    }else{
    break;
    }
    }
    int[] a=new int[count];
    for(int i=0;i<count;i++){ //数组赋值,登入数字,注意是从个位提取的而不是最高位
    a[i]=temp%10;
    temp=temp/10;
    }
    System.out.println("逆转后:"); //输出
    for(int i=0;i<count;i++){
    System.out.print(a[i]);
    }
    int c=1;
    temp=0;
    for(int i=count-1;i>=0;i--){ //将数组转化为一个实际数字,还原
    number=a[i]*c;
    c=c*10;
    temp=temp+number;
    }
    System.out.print("\n");
    System.out.print(temp);[\quote]