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