Java instance exercises, java instances

Source: Internet
Author: User

Java instance exercises, java instances

1. Do not use the intermediate variable to swap two numbers

Public class Exchange {public static void main (String [] args) {role = new role (System. in); System. out. println ("enter an integer:"); Long num1 = numeric. nextLong (); System. out. println ("enter an integer:"); Long num2 = numeric. nextLong (); System. out. println ("pre-exchange num1:" + num1 + "pre-exchange num2:" + num2); num1 = num1 ^ num2; num2 = num2 ^ num1; num1 = num1 ^ num2; system. out. println ("num1:" + num1 + "num2:" + num2);} Exchange

2. Use the Three-object operator to determine parity

Public class ParityCheck {public static void main (String [] args) {custom partition = new partition (System. in); System. out. println ("enter an integer:"); Long num = bytes. nextLong (); String s = (num % 2 = 0 )? "This number is: even number": "This number is: Odd Number"; System. out. println (s) ;}} ParityCheck

 

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.