Compile and run two variable value exchange methods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.