输入一个2位数,怎么样将其个位和十位互换位置?

解决方案 »

  1.   

    把数字转换为char数组,调整数字位置,再转换回来就Ok了。
      

  2.   

    使用subString()截取,然后交换即可。
      

  3.   


    public static void main(String[] args) {
    int shuzi=48;
    String i = String.valueOf(shuzi);
    char c[] = new char[2];
    c[0]=i.charAt(0);
    c[1]=i.charAt(1);
    String daox="";
    for (int j = c.length-1; j >=0; j--) {
    daox=daox+c[j];
    }
    int k=Integer.parseInt(daox);
    System.out.println(k);
    }
      

  4.   

    public class TestTest {    public static void main(String[] args) {
            int num = 12;
            int reverse = num % 10 * 10 + num / 10;
            System.out.println(reverse);
        }
    }
      

  5.   

    最简单的:System.out.println(value/10 + value%10*10);
      

  6.   


                    int num = 21;
    StringBuffer sb = new StringBuffer();
    sb.append(num);
    System.out.println(sb.reverse().toString());
      

  7.   


    int test = 12;

    System.out.println(new StringBuffer(String.valueOf(test)).reverse().toString());
    试试这个
      

  8.   

            int zs=12;
            StringBuffer sb = new StringBuffer(Integer.toString(zs));
            int ds=Integer.parseInt(sb.reverse().toString()) ;
            System.out.print(ds);
      

  9.   

    package com.xlh.dao;public class Math { public static void main(String args[]) {


    int i = 12 ;

    //第一种
    System.out.println(new StringBuffer(String.valueOf(i)).reverse().toString()) ;
    //第二种很强
    System.out.println(i%10*10+i/10) ;
    //第三种一开始就能想到的
    String str = String.valueOf(i) ;
    char[] t = new char[str.length()] ;
    t = str.toCharArray() ;
    str = "" ;
    for (int _i=0;_i<=t.length-1;_i++) {
         str += t[t.length-1-_i] ;
    }
    System.out.println(str) ;
    //第四种substring
    String _str = String.valueOf(i) ;
    int len = _str.length() ;
    for (int _i=0;_i<=len-1;_i++) {
    System.out.println(_str.substring(len-1-_i,len-_i)) ;
    }


    }
    }
      

  10.   

    就针对2位数来说,5、6楼的方法已经很好了。
    再继续推算一下,很有意思:
    假设有一个2位数10a+b,则有 10b + a = b + 9b + 10a - 9a = 10a + b + 9(b-a)
        即  互换后的数 = 原数 + (个位数字 - 十位数字)× 9比如原数12,则有12 + (2 - 1)×9 = 21  
       原数91,则有91 + (1 - 9)×9 = 19借用5楼的代码:
    public class TestTest {     public static void main(String[] args) { 
            int num = 12; 
            int reverse = num + (num % 10 - num / 10)*9; 
            System.out.printf("%02d",reverse); //便于90 输出 09
        } 
    }