java程序代码 Exchenge.java

//Exchange.java

class Exchange
{
 public static void main (String args[])
 {
  int num1 = 10, num2 = 20, temp;
  //perform a traditional exchange. this requires the use of a
  //temporary variable. any data types can be exchagned.
  
  System.out.println("Traditional Exchange");
  
  
  temp = num1;
  num1 = num2;
  num2 = temp;
  
  System.out.println("num1 = "+ num1);
  System.out.println("num2 = "+ num2);
  
  //reset variables to original values.
  
  num1 = 10;
  num2 = 20;
  
  //perform an additive exchange. no temporaty variable if needed. only numeric types can be exchanged.
  
  System.out.println("\nAdditive Exchange");
  
  num1 += num2;
  num2 = num1 - num2;
  num1 -=num2;
  
  System.out.println ("num1 = " + num1);
  System.out.println ("num2 = " + num2);
  
  //reset variables to original values.
  
  num1 = 10;
  num2 = 20;
  
  
  //perform a bitwise exclusive ro exchange.no temporary variable is needed. all numeric types except floating-
        // point and double-precision floating-point can be exchanged.
       
        System.out.println("\nBitwise exclusive OR Exchange");
       
       
        num1 ^= num2;
        num2 =num1 ^ num2;
        num1 ^= num2;
       
       
        System.out.println("num1 = " + num1) ;
        System.out.println("num2 = " + num2);  
 }
}

原文地址:https://www.cnblogs.com/josn1984/p/395374.html