Java Learning Phase III: operator and structure learning

Source: Internet
Author: User
Tags bitwise operators

Import Java.util.Scanner;
/*
* Learning of operators in Java:
* Arithmetic operators: + 、-、 *,/and%, two integers, result or integer.
* Assignment operators: =, + =,-=, *=,/=,%=, &=, |=, ~=, ^=, <<=, >>=, >>>=, + +, and--。
* Comparison operators: = =,! =, <, <=, >, and >=.
* Logical operator:&&, | | And!.
* Bitwise operators:&, |, ~, ^, <<, >>, and >>>.
*
*/
public class Test {

public static void Main (string[] args) {
/*
* Function Application One: An integer in binary output (adjustable bits number)
*/
int Shuzi;
Scanner s=new Scanner (system.in);//Get console input
System.out.println ("Please enter an integer");
Find and return the next number from this console
Shuzi=integer.parseint (S.next ()); string conversion to Integer type

for (int i=7;i>=0;i--) {//i=7, representing 8-bit 2 binary i=31 32-bit binary
if ((shuzi& (1<<i))!=0) {
System.out.println ("1");
}else{
System.out.println ("0");
}

}
S.close ();
/*
* Function Application Two: Determine the number of digits of a number
*/
int q=1234578;
String str=q+ ""; Convert an integral type to a string first
System.out.println ("number" +str+ "is:" +str.length () + "number of digits");

/*
* Function Application Three: Reverse the output of a number
*/
int olddata=12345678;
int count= (olddata+ ""). Length ();
int newdata=0;
while (olddata!=0) {
for (int i=0;i<count;i++) {
newdata=newdata*10+olddata%10;
OLDDATA=OLDDATA/10;
}
System.out.println ("Newdata=" +newdata);


}
/*
* Function Application Four: Determine whether a number is a prime
*/
int num=3;
Boolean flag=true; The default is prime number
for (int i=2;i<=num/2;i++) {
if (num%i==0) {

Flag=false; If an even number jumps out of the loop
Break

}
}
if (flag) {
System.out.println (num+ "is a prime number!");
}else{
System.out.println (num+ "is not a prime number!");
}

}}

Java Learning Phase III: operator and structure learning

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.