Java Basics/15th lesson: Operators/All Java Operators 4

Source: Internet
Author: User

2018-03-07

1. Assignment operation

The operation order of the assignment is right-to-left: int i = 5+5;
First the 5+5 operation is performed, the result 10 is obtained, and the value of 10 is assigned to I

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        int i = 5+5;    }}
2. Perform an operation on itself and assign a value

+ = is self-added: i+=2; Equivalent to: i=i+2;
Other--=, *=,/=,%=, &=, |=, ^=, >=, >>>= are similar, do not repeat

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        int i =3;        I+=2;        System.out.println (i);                  int j=3;        J=j+2;        System.out.println (j);          }}

Second, use scanner to read integers

So far, we have learned to use SYSTEM.OUT.PRINTLN ("") to output data to the console.
In the next exercise, you need to enter data from the console, so you need to use the scanner class

1. Reading integers using scanner

Note: Using the scanner class, you need to precede the first to import this class in order to work properly:

import Java.util.Scanner;

Importpublicclass  HelloWorld {    publicstatic   void  Main (string[] args) {        new  Scanner (system.in);         int a = s.nextint ();        System.out.println ("First integer:" +a);         int b = s.nextint ();        System.out.println ("second integer:" +B);}    }
2. Use scanner to read floating-point numbers

Import Java.util.Scanner;    Public class HelloWorld {    publicstaticvoid  main (string[] args) {          New  Scanner (system.in);         float a = s.nextfloat ();        SYSTEM.OUT.PRINTLN ("read the value of the floating-point number is:" +a);}     }
3. Reading a string using scanner

Import Java.util.Scanner;    Public class HelloWorld {    publicstaticvoid  main (string[] args) {          New  Scanner (system.in);         = s.nextline ();        System.out.println ("The string read is:" +a);}    }
4. After reading the integer, then read the string

Note that if you read the integer through Nextint () and then read the string, the return line is read: "\ r \ n" because Nextint only reads the digital information, but does not read the return line "\ r \ n".

Therefore, if you need to read the integer in the business, then read the string, then you should execute two times nextline (), the first time is to take the integer, the second is to read the real string

Import Java.util.Scanner;     Public class HelloWorld {    publicstaticvoid  main (string[] args) {          New  Scanner (system.in);         int i = s.nextint ();        System.out.println ("The integer Read is" + i);         = s.nextline ();         = s.nextline ();        System.out.println ("The string read is:" +a);}    }

Java Basics/15th lesson: Operators/All Java Operators 4

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.