Java Process Control
The syntax of Java Process Control is similar to C + +, there are if...else, while, Do...while, for, Switch...case, and so on, here no longer describe the specific syntax, just for illustrative.
Output 99 multiplication table (upper right triangle):
public class Demo {public
static void Main (string[] args) {
int i, J;
For (I=1 i<=9; i++) {
for (j=1; j<=9; J + +) {
if (j<i) {
//print eight spaces, remove the space is the upper left Triangle
System.out.print ( " ");
} else{
System.out.printf ("%d*%d=%2d", I, J, i*j);
}
System.out.print ("\ n");}}
Run Result:
1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*2=
4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5*5=25 5*6=30 5*7= 5*8=40 5*9=45
6*6=36 6*7=42 6*8=48 6*9=54 7*7=49 7*8=56 7*9=63 8*8=64 8*9=72 9*9=81
There is also a printf () statement in Java that controls the output format, but is not commonly used in actual development because the data output to the console rarely requires a strict format, and General println () and print () are sufficient.
println () The output text wraps, print () does not wrap.
For example, for a certain month of a certain year, how many days:
Import java.util.*;
public class Demo {public
static void Main (string[] args) {
int days = 0;
Get user input
Scanner sc = new Scanner (system.in);
System.out.print ("Enter year:");
int year = Sc.nextint ();
System.out.print ("Enter month:");
int month = Sc.nextint ();
Switch (month) {case
1: Case
3: Case
5: Case
7: Case
8:
case A: Days =31;
break;
Case 4: Case
6: Case
9:
days=30;
break;
Case 2:
//Judgment Leap year
if (year%4==0 && year%100!=0 | | year%400==0)
days=29;
else
days=28;
break;
Default:
System.out.println ("Month input error!)" ");
System.exit (0); Force end Program
}
System.out.printf (days:%d\n);}
Run Result:
Enter year: 2014
enter month:
days: 28
Java does not like the C language scanf () statement, from the console to get input a bit troublesome, I recommend the use of Scanner class, the specific syntax for everyone to see the API.
Java operators
The operators in Java are about the same as C + +.
Mathematical operators
Mathematical operation, the result is a numerical value. See table below:
Relational operators
Relational operator, and the result is a Boolean value. See table below:
Bitwise operators
Bitwise operators perform a bitwise logical operation on the binary form of an integer to get an integer. See table below:
Conditional operator
There is also a conditional operator (three-mesh operator) in Java:
Condition is a Boolean value. According to condition, X1 or X2 values are taken.
Here is a comprehensive example of an operator:
public class Demo {public static void main (string[] args) {int a=10;
int b=10;
int x=10;
int y=21;
int z=10;
System.out.println ("a=" + (a++));
System.out.println ("A value a=" + a);
SYSTEM.OUT.PRINTLN ("Ex-plus b=" + (++b));
System.out.println ("---------------------"); System.out.println ("Say X>y, right?")
"+ (x>y)); System.out.println ("Think X>y and X<y, right?")
"+ ((x>y) && (x<y));" System.out.println ("Think x>=y or x==y, right?") "+ ((x>=y) | |
(x==y))); System.out.println ("Think X<y or x=z, right?") "+ ((x<y) | |
(x==z)));
System.out.println ("---------------------");
System.out.println ("A&x's result is:" + (a&x));
System.out.println ("A|x's result is:" + (a|x));
System.out.println ("Y^z's result is:" + (y^z));
System.out.println ("---------------------");
System.out.println ("A left 2-bit result is:" + (A<<2));
System.out.println ("Y-shift 3-bit result is:" + (y>>3)); }
}
Run Result:
After
the value of a=10 a a=11 from
add b=11
---------------------
said X>y, right? False
thinks x>y and X<y, right? False
thinks x>=y or x==y, right? False
thinks x<y or x=z, right? True---------------------The result of A&x is: The result of a|x: one
y^z result
is:---------------------
The result of a left 2-bit shift is that
the 3-bit shift to the right of Y is: 2