小明左手一张10  右手一张8 要互换手中的牌。2个整数分别保存在两个变量中,将这两个变量的直互换,打印输出互换后的结果!

解决方案 »

  1.   

    public void exchange(int a,int b){
        private int c;
        c = b;
        b = a;
        a = c;
        System.out,println(a+"\n"+b);
    }
      

  2.   


    public class Test1Server {

    public static void main(String[] args) {
    Test1Server test = new Test1Server();
    test.exchange(10, 8);
    }
    void exchange(int leftHand,int rightHand ){
    int x ;
    x  =  leftHand;
    leftHand = rightHand;
    rightHand = x;
    System.out.print(leftHand);
    System.out.print(rightHand);
    }
    }
      

  3.   

    a += b;
    b = a-b;
    a -= b;
    有溢出危险。
      

  4.   

    c = b;
    b = a;
    a = c;
      

  5.   


    public void exchange(int a,int b){
      a ^= b;
      b ^= a;
      a ^= a;
      System.out.println( a + "\n" + b );
    }没有溢出危险
      

  6.   


    public class StudentHome {
    int leftHand, rightHand;

    StudentHome(int leftHand, int rightHand){
    this.leftHand = leftHand;
    this.rightHand = rightHand;
    System.out.println("Before:  leftHand=" + leftHand + "  rightHand=" + rightHand);
    }

    public void exchange() {
    this.leftHand = this.leftHand ^ this.rightHand;
    this.rightHand = this.rightHand ^ this.leftHand;
    this.leftHand = this.leftHand ^ this.rightHand;

    System.out.println("After:  leftHand=" + leftHand + "  rightHand=" + rightHand);
    }

    public static void main(String args[]) {
    StudentHome xiaoming = new StudentHome(10,8);
    xiaoming.exchange();
    }
    }不知道是不是楼主要的
      

  7.   

    上边写错了public void exchange(int a,int b){
      a ^= b;
      b ^= a;
      a = a^b;
      System.out.println( a + "\n" + b );
    }
      

  8.   

    public void exchangeCard(int left, int right) {
    System.out.println("" + left + right);
    int temp = left;
    left = right;
    right = temp;
    System.out.println("" + left + right);
    }
      

  9.   

    c = b;
    b = a;
    a = c;
    这个最简单 也最容易理解了 尤其刚入门的
      

  10.   

    public class A
    {
    public static void main ( String [] args )
    {
    int a = 8 ;
    int b = 10 ;
    int c = 0 ;
    System.out.println("小明现在左手的是" +  a);
    System.out.println("小明现在you手的是" +  b);

    c = b;
    b = a;
    a = c;

    System.out.println("小明现在左手的是" +  b);
    System.out.println("小明现在有手的是" +  a);
    }
    }这应该是最简单的了吧
      

  11.   

    public class Test {
        
        public static void main(String[] args) {
            int x ,leftHand=10,rightHand=8;
            x  =  leftHand;
            leftHand = rightHand;
            rightHand = x;
            System.out.print(leftHand);
            System.out.print(rightHand);
        }
    }