1. Use different or running methods
With the data defined above, you can use the code
A = A ^ B; // execution variable interchange B = B ^ A; A = A ^ B; System. out. println ("A =" + A + "\ tB =" + B );
Use an exclusive or to exchange two values of A and B;
From the input value to the exchange value, the detailed code is as follows:
Package com. lixiyu; import java. util. required; public class VariableExchange {public static void main (String [] args) {using A = new using (System. in); System. out. println ("Enter the value of variable A:"); Long line =. nextLong (); records B = new records (System. in); System. out. println ("Enter the value of variable B:"); Long line1 = B. nextLong (); System. out. println ("A =" + line); System. out. println ("B =" + line1); System. out. println ("convert values"); line = line ^ line1; line1 = line1 ^ line; line = line ^ line1; System. out. println ("A =" + line + "\ tB =" + line1 );}}
Note that this method is suitable for numeric type. Use Long line = A. nextLong () instead of String.
2. Define a third-party variable for exchange
Implementation using the assignment method principle
For example, to implement A and B interchange: Define A variable C, C = A; A = B; B = C;
Package com. lixiyu; import java. util. required; public class VariableExchange {public static void main (String [] args) {String line2; required A = new feature (System. in); System. out. println ("Enter the value of variable A:"); String line =. nextLine (); records B = new records (System. in); System. out. println ("Enter the value of variable B:"); String line1 = B. nextLine (); System. out. println ("A =" + line); System. out. println ("B =" + line1); System. out. println ("execute conversion"); line2 = line; line = line1; line1 = line2; System. out. println ("A =" + line + "\ tB =" + line1 );}}
Compile and run successfully. This method is not only applicable to Long type but also other types, such as String type)
Other methods will be used later.
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1302809