1. Sequential sentences
Statements: Code separated by semicolons is called a statement
Note: When no code is written but a semicolon, it is also a statement, called an empty statement
Two, judgment (if… else)
Note: If there is only one statement in the curly braces, the curly braces can be omitted without writing
Format one:
if(judgment condition){
Executed code block 1;
Executed code block 2;
…;
the executed code block n;
}
Format two:
if(judgment condition){
Executed code block 1;
…;
the executed code block n;
}else{
Executed code block 1;
…;
the executed code block n;
}
Format three:
if(judgment condition 1){
Executed code block 1;
} else if (judgment condition 2) {
execute statement;
} else if (judgment condition 3) {
execute statement;
}
example:
import java.util.Scanner; //Import the package import java.util.Scanner, use the Scanner class, the fixed usage Scanner sc=new Scanner(System.in), int nextInt = sc.nextInt() to get the number entered by the user public class Demo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a number: "); int nextInt = sc.nextInt (); if (nextInt %2 == 0) { System.out.println(nextInt + ": is an even number"); } else { System.out.println(nextInt + ": is an odd number"); } System.out.println("over"); } } //operation result [root@bch04 java]# javac Demo1.java [root@bch04 java]# java Demo1 Please enter a number: 3 3: is an odd number over
[root@bch04 java]# cat Demo2.java import java.util.Scanner; public class Demo2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a number: "); double score = sc.nextDouble(); char grade; if (score >=90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 60) { grade = 'C'; } else { grade = 'D'; } System.out.println(grade); } } //operation result [root@bch04 java]# java Demo2 Please enter a number: 78.4 C
import java.util.Scanner; public class Demo3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the year: "); int year = sc.nextInt(); boolean isLeapYear = (year %4 == 0); isLeapYear = isLeapYear && (year %100 != 0); isLeapYear = isLeapYear || (year %400 ==0); if (isLeapYear) { System.out.println(year +": is the run year"); } else { System.out.println(year +": is a normal year"); } } } //operation result [root@bch04 java]# java Demo3 Please enter the year: 2018 2018: is a normal year
3. Select the judgment statement (switch)
Format:
switch(expression) { case takes value 1: execute statement; break; case takes value 2: execute statement; break; …... default: execute statement; break; } |
Features of switch statement:
1) There are only four types selected by the switch statement: byte, short, int, char;
2) There is no order between cases and default, first judge all cases, and execute default if there is no matching case;
3) The condition for the switch statement to stop is: encounter the break keyword or the braces that end the switch statement;
4) If the matching case or default does not have a corresponding break, then the program will continue to execute downward until it encounters the break or the end of the switch;
5) The value in the switch case must have the same data type as the value of the switch expression
example:
[root@bch04 java]# cat Demo4.java import java.util.Scanner; public class Demo4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double x = sc.nextDouble(); String z = sc.next(); double y = sc.nextDouble(); switch (z) { case "+": System.out.println("x+y=" +(x+y)); break; case "-": System.out.println("x-y=" +(x-y)); break; case "*": System.out.println("x*y=" +(x*y)); break; case "/": System.out.println("x/y=" +(x/y)); break; default: System.out.println("Not reliable"); break; } } } //operation result [root@bch04 java]# java Demo4 4 + 5 x+y=9.0
Fourth, while loop
Description: Judge the condition first, and execute the loop body only if the condition is met
Format:
while(conditional expression)
{
execute statement;
}
example
[root@bch04 java]# cat Demo5.java import java.util.Scanner; public class Demo5 { public static void main(String[] args) { int num = (int)(Math.random()*100)+1; Scanner sc = new Scanner(System.in); int guessNum = -1; while (guessNum != num) { System.out.println("Please enter a number between 1-100: "); guessNum = sc.nextInt(); if ( guessNum == num) { System.out.println("In!"); } else if ( guessNum > num) { System.out.println("The value is too big!"); } else { System.out.println("The value is too small!"); } } } } //operation result [root@bch04 java]# javac Demo5.java [root@bch04 java]# java Demo5 Please enter a number between 1-100: 12 Value is small! Please enter a number between 1-100:
Five, do while loop
Description: Execute the loop body first, then judge the condition, if the condition is satisfied, then continue to execute the loop body
Features: Whether the condition is met or not, the loop body is executed at least once
Format
do { execute statement; }while(conditional expression); |
example
[root@bch04 java]# cat Demo6.java import java.util.Scanner; public class Demo6 { public static void main(String[] args) { int num = (int)(Math.random()*100)+1; Scanner sc = new Scanner(System.in); int guessNum = -1; int count = 0; do { System.out.println("Please enter a number between 1-100: "); guessNum = sc.nextInt(); if ( guessNum == num) { System.out.println("In!"); } else if ( guessNum > num) { System.out.println("The value is too big!"); } else { System.out.println("The value is too small!"); } count++; } while ( guessNum != num); System.out.println("The number to guess is: " +num +" You guessed a total of: " +count + "times"); } } //operation result [root@bch04 java]# javac Demo6.java [root@bch04 java]# java Demo6 Please enter a number between 1-100: 10 Great value! Please enter a number between 1-100: 5 Great value! Please enter a number between 1-100: 4 Great value! Please enter a number between 1-100: 2 bingo! The number to guess is: 2 You guessed a total of: 4 times
Six, for loop
Format:
for(initialization expression; loop condition expression; operation expression after loop) {
execute statement;
}
example:
[root@bch04 java]# cat Demo7.java public class Demo7 { public static void main(String[] args) throws InterruptedException { for (int i=1;i<=9;i++) { for (int j=1;j<=i;j++) { System.out.print(i +"*" +j +"=" +(i*j) +"\t"); Thread.sleep(1000); } System.out.println(" "); } } } //operation result [root@bch04 java]# javac Demo7.java [root@bch04 java]# java Demo7 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
Seven, break, continue keywords
break: the statement following the termination statement
continue: jump out of this loop and execute the next loop
Note: if continue appears at the end of the loop (the last statement), then it can be omitted