1: Keyboard input data
1. Guide Package: import Java util. Scanner;
2. Create object: Scanner sc = new Scanner(System.in);
3. Received data: data type x=sc.next data type () example: int x=sc.nextInt();
Character type: String x=sc.next();
import java.util.Scanner; public class Practice { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a= sc.nextInt(); } }
2: Process control classification
1. Sequence structure: execute code from top to bottom
2. Select structure
(1) if statement: if (relational expression 1){
Statement body 1;
} else} if (relational expression 2){
Statement body 2;
}
...
else {
Statement body n+1;
}
Example: get the maximum value of three data
import java.util.Scanner; public class Practice { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Please enter the first data:"); int a= sc.nextInt(); System.out.println("Please enter the second data:"); int b= sc.nextInt(); System.out.println("Please enter the third data:"); int c= sc.nextInt(); if (a>b){ //Nested implementation of if statements if(a>c){ System.out.println("The maximum value is:"+a); }else { System.out.println("The maximum value is:"+c); } }else { if(b>c){ System.out.println("The maximum value is:"+b); }else { System.out.println("The maximum value is:"+c); } } } }
(2) switch statement format: value of expression: byte,short,int,char # jdk1 After 5, you can enumerate jdk1 After 7, it can be String
Switch (expression){
case value 1:
Statement body 1;
break;
case value 2:
Statement body 2;
break;
...
default:
Statement body n+1;
break;
}
Note: case can only be followed by constants, not variables, and the values after multiple cases cannot have the same value.
Default and bear can be omitted according to the situation, and are generally not recommended; The position of default can be anywhere in the switch statement.
For example: output the corresponding Monday, Tuesday, Wednesday... Sunday according to the values 1, 2, 3,... 7 entered on the keyboard.
import java.util.Scanner; public class Spas { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean flag = true; while (flag) { //If the correct number is not entered, while will loop through the switch statement and will not stop. System.out.println("Please enter a number:"); int n = sc.nextInt(); switch (n) { case 1: System.out.println("It's Monday"); flag = false; break; case 2: System.out.println("It's Tuesday"); flag = false; break; case 3: System.out.println("It's Wednesday"); flag = false; break; case 4: System.out.println("It's Thursday"); flag = false; break; case 5: System.out.println("It's Friday"); flag = false; break; case 6: System.out.println("It's Saturday"); flag = false; break; case 7: System.out.println("It's Sunday"); flag = false; break; default: System.out.println("Please enter a correct number"); break; } } } }
3. Circulation structure
(1) for loop: for (initialization statement; judgment condition statement; control condition statement) {/ / judge whether the condition statement is a Boolean expression, and judge whether the loop continues to execute the loop body statement.
Loop body statement;
After the} / / for loop ends, the variables defined in the initialization statement cannot be accessed externally
Example: narcissus number: the so-called narcissus number refers to a three digit number, and the cubic sum of each digit is equal to the number itself. (153 = 1*1*1 + 5*5*5 + 3*3*3 )
Code examplepublic class ForPractice { public static void main(String[] args) { int geWei; int shiWei; int baiWei; for(int i=100;i<1000;i++){ geWei=i%10; //First find the digits of i shiWei=i/10%10; baiWei=i/100; if(i==geWei*geWei*geWei+shiWei*shiWei*shiWei+baiWei*baiWei*baiWei){ System.out.println(i+"It's a daffodil number"); } } } }
(2) while loop: while (judgment condition statement) {
Loop body statement;
}
Example: the highest mountain in China is Mount Everest: 8848m. Now I have a large enough paper with a thickness of 0.01m. How many times can I fold it to ensure that the thickness is not lower than the height of Mount Everest?
public class WhilePractice { public static void main(String[] args) { double i=0.01; int t=0; while (i<=8848){ i=i*2; t++; } System.out.println(t); } }
(3) do while loop: do {
Loop body statement;
} while ((judgment condition statement);
/ / do... While loop will also execute the loop body once when the judgment condition is not tenable; for loop and while loop will execute the loop body only when the condition is true.
Example: output helloworld;
public class DoWhileDemo1 { public static void main(String[] args) { int x = 3; do{ System.out.println("HelloWorld"); }while (x<3); } }