Outline:
One, branch structure
If
Switch
Second, the circulation
For
While
Do While
Break
Continue
Third, formatted output "printf"
--------------------------------------------
Switch statement
It is also used for branching, and if it is different, it can only do equivalent comparisons.
Switch can complete the business, if can also be done.
Grammar:
Switch (variable factor) {
Case value 1:
[code block;]
[Break;]
Case Value 2:
[code block;]
[Break;]
...
Case Value N:
[code block;]
[Break;]
[Default:]
[code block;]
[Break;]
}
Note:
The types of variable factors are:
Byte, char, short, int
Enumerate "JDK5.0"
String "JDK7.0"
Case:
Prompts the user to enter the year and month from the keyboard, and outputs the maximum number of days of this month for this year.
Tips:
Java.util.Scanner
Variable
====================
Supplemental operators
Member access operators
.
Assignment operators
=
Arithmetic operators
+,-,*,/, %, ++, --, +=,-=,*=,/=,%=
Comparison operators
, <, >=,<=, = =,! =
logical operators
&&, | |,!
Bitwise operators
& Bitwise AND
| bitwise OR
~ Bitwise REVERSE
^ Bitwise XOR OR
>> right Shift "high fill sign bit, 0 for positive, 1 for negative"
>>> unsigned Right shift "high always 0"
<< left Shift
&=, |=, ^=, ~=, >>=, >>>=, <<=
Such as:
int i = 13;
int j = 12;
int result = i & j; 12
In-system:
Binary, consisting of 0 and 1
Note: The data stored in the computer is in binary form.
There is no binary literal in Java
Octal, "0,7"
Note:
Java has 8 binary literals, starting with 0
Such as:
int i = 017; OK, equivalent to 10 binary: 15
Decimal, [0,9]
> 2: In addition to 2 methods
Such as:
i = 19; = "10011
2-10: The method of adding the sum of the exponentiation
Such as:
Suppose there is a binary string: 1010110, which corresponds to 10 binary: 86
Hex "0,9a,z"
Note: There are 16 binary literals in Java, beginning with 0 x or 0x
Such as:
int i = 0x1F; OK, equivalent to 10 binary 31
-------------
Bitwise operators apply the port:
Clear 0 take bits with, a position of 1 is available or;
To take the inverse and swap, lightly use XOR.
--
Case:
For example: Given any integer, remove the last 1 bits.
You need to specify a mask [mask], this mask = 1.
int i = ...;
int mask = 1; 1
int result = i & mask; This makes it possible to get the last 1 bits of the integer i.
Similarly, what if the lower 3 bits are taken? Mask should be 7.
mask = 7; 111
--------------
The inverse operator is the inverse of the "destructive" nature and is for all bits.
int i = 5;
int j = ~i; j =-6
If the XOR is used, it can be directed against the "point-and-locate"
000000000 ... 0101
Just set the mask to 0xf[1111] to reverse the back 4 bits. Other bits unchanged
---------------------
Exchange:
Theorem: Any integer with another integer is continuously different or 2 times, and its value is unchanged.
==============================
For loop
Grammar:
for (Expression1;expression2;expression3) {
Loop body
}
Note:
Expression1 represents a looping initialization statement that executes only 1 times
Expression2 represents the Loop condition judgment statement, which executes 1 times per loop 1 times
Expression3 represents a cyclic factor change statement that executes 1 times per loop 1 times
All three of these expressions can be omitted, such as:
for (;;) {
Dead loop
}
such as: Cycle 10 times
for (int i=0;i<10;i++) {
System.out.println ("I" +i);
}
Or:
int i = 0;
for (; i<10;i++) {
System.out.println ("I" +i);
}
==============
While loop
Grammar:
while (conditional expression) {
Loop body
}
Note:
When the conditional expression is set, the loop body is executed. Then judge, and so on.
Such as:
while (true) {
Dead loop
}
==
If you can determine the number of cycles, prioritize the use for
Conversely, do not determine the loop several times, prefer to use while
--
If you use while loop 10:
int i = 0;
while (I < 10) {
System.out.println ("i =" +i);
i + +;
}
--
Case:
The loop generates a random number [1,100], and when the generated random number is exactly 50 o'clock, the loop exits,
The second output was generated several times in 50.
How do I generate random numbers?
1). Using the Java.util.Random class
Code:
Random ran = new random ();
int n = ran.nextint (100) + 1; Generate random numbers
2). Using the random method in the Math class
Code:
int n = (int) (Math.random () *100) +1;
---
Java Note 3-for,switch Loop