1. Input and output
When a Scanner is created through new Scanner (system.in), the console waits for input until the hit key is finished and the input is passed to Scanner as the scanned object. If you want to get the input, you only need to call Scanner's Nextline () method. The code is as follows:
Scanner input = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Hint: enter");
int a = Input.nextint ();
2. Basic statement
If statement: if (Boolean expression) {
If the Boolean expression is ture, the statement executed
}
Example:
Scanner input = new Scanner (system.in);
System.out.println ("Enter a number");
int a = Input.nextint ();
if (a > 10) {
System.out.println (a);
}
If Else statement: if (Boolean expression) {
Executes when the Boolean expression is ture
}else{
Executed when the Boolean expression is wai false
}
Example:
Scanner input = new Scanner (system.in);
System.out.println ("Enter a number");
int a = Input.nextint ();
if (a > 10) {
System.out.println (a);
}else{
System.out.println (a);
}
Switch statement:
Switch (variable) {
Case Value:
Break
Case Value:
Break
Default
Break
}
For loop://number of known cycles
for (initialize; Boolean expression; update) {
Statement
}
Example: 9*9 law
for (int a = 1; a <= 9; a++) {
for (int b = 1; b <= A; b++) {
int c = A * b;
System.out.print (b + "*" + + "=" + c+ "");
}
System.out.println ();
}
While loop://Unknown loop count
while (Boolean expression) {
Statement
}
Example:
int a=1;
while (a<=10) {
System.out.print (A + "");
a++;
}
Break statement://Terminate current loop
Example:
int k=1;
while (k<=10) {
System.out.print (k + "");
if (k==6) {
Break
}
k++;
}
Continue statement://Exits the current loop, but does not affect the loop behind it
Example:
System.out.print ("for Loop");
System.out.println ();
for (int i = ten; i > 0; i--) {
if (i% 2 = = 0) {
Continue
}system.out.print (i+ "");
}
JAVA Getting Started programming