<> Language foundation day 4 :
<> review :
* operator :
* arithmetic :+,-,*,/,%,++,–
* relationship :>,<,>=,<=,==,!= boolean
* logic :&&,||,! boolean
* assignment :=,+=,-=,*=,/=,%=
* String connection :+
* condition / Three eyes :boolean? number 1: number 2
* Branching structure : Conditionally executed statements
* if structure :1 Road
* if…else structure :2 Road
<> note :
*
Scanner Receive user input data : common 3 step , No need to understand , Recite it first
* stay package lower :
import java.util.Scanner;
* stay main in :
Scanner scan = new Scanner(System.in);
* In the first 2 Step down :
System.out.println(“ Please enter age :”);
int age = scan.nextInt();
System.out.println(“ Please enter the commodity price :”);
double price = scan.nextDouble(); package day04; import java.util.Scanner;
//1. Import a scanner //Scanner Presentation of public class ScannerDemo { public static void main(
String[] args) { Scanner scan = new Scanner(System.in); //2. Create a new scanner System.out.
println(" Please enter age :"); int age = scan.nextInt(); //3. Scan an integer to age System.out.println(
" Please enter the commodity price :"); double price = scan.nextDouble(); //3. Scan a decimal to price System.out.
println(" Age is :"+age+", Price is :"+price); } }
*
Branching structure :
*
if…else if structure : Multiple roads
* grammar :
if(boolean-1){
Statement block 1
}else if(boolean-2){
Statement block 2
}else if(boolean-3){
Statement block 3
}else{
Statement block 4
}
* Execution process :
judge boolean-1, if it is true Execute the statement block 1( end ), if it is false be
Re judgment boolean-2, if it is true Execute the statement block 2( end ), if it is false be
Re judgment boolean-3, if it is true Execute the statement block 3( end ), if it is false be Execute statement block 4( end )
* explain :
Statement block 1/2/3/4, Only one of them can be executed ------------ Multiple choice 1 public class ScoreLevel { public static
void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.
println(" Please enter your grade :"); double score = scan.nextDouble(); // Band number (-25,888,95,85,65,45)
if(score<0 || score>100){ System.out.println(" Illegal results "); }else if(score>=90){ // legitimate
System.out.println("A- excellent "); }else if(score>=80){ System.out.println("B- good "); }
else if(score>=60){ System.out.println("C- secondary "); }else{ System.out.println(
"D- fail, "); } } }
*
switch…case structure : Multiple roads
advantage : efficient , Clear structure
shortcoming : Equality can only be judged for integers
break: Jump out switch
Common interview questions :switch What type of data can it act on
-------------------------byte,short,int,char,String, Enumeration type
public class CommandBySwitch { public static void main(String[] args) {
Scanner scan= new Scanner(System.in); System.out.println(" Please select a function : 1. withdraw money 2. deposit
3. Check the balance 0. Return card "); int command = scan.nextInt(); switch(command){ case 1: System.out
.println(" Withdrawal operation ..."); break; case 2: System.out.println(" Deposit operation ..."); break; case 3
: System.out.println(" Query balance ..."); break; case 0: System.out.println(" Card return operation ...");
break; default: System.out.println(" Input error "); } } }
*
loop : Execute the same or similar code repeatedly
*
Three elements of circulation :
* Initialization of loop variables
* Conditions of circulation ( Based on cyclic variables )
* Change of cyclic variable ( Towards the end of the cycle )
Cyclic variable : The number changed repeatedly throughout the cycle
// run 3 circle : Cyclic variable : Number of laps run count 1)int count=0; 2)count<3 3)count++; count=0/1/2/ 3 End of time The number of turns is
0 enough 3 Circle ? Not enough Run a lap The number of turns is 1 enough 3 Circle ? Not enough Run a lap The number of turns is 2 enough 3 Circle ? Not enough Run a lap The number of turns is 3 enough 3 Circle ? Enough // Printer printing 6 Resume :
Cyclic variable : Number of copies num 1)int num=0; 2)num<6 3)num++; num=0/1/2/3/4/5/ 6 End of time The number of copies is 0 enough 6 Share ? Not enough
Print a copy The number of copies is 1 enough 6 Share ? Not enough Print a copy The number of copies is 2 enough 6 Share ? Not enough Print a copy The number of copies is 3 enough 6 Share ? Not enough Print a copy The number of copies is 4 enough 6 Share ? Not enough
Print a copy The number of copies is 5 enough 6 Share ? Not enough Print a copy The number of copies is 6 enough 6 Share ? Enough
*
Cyclic structure :
*
while structure : Judgment before execution , It's possible not to do it once
*
grammar :
while(boolean){
Statement block ------------- Repeatedly executed code
}
*
Execution process :
judge boolean Value of , if it is true Execute the statement block ,
Re judgment boolean Value of , if it is true Then execute the statement block ,
Re judgment boolean Value of , if it is true Then execute the statement block ,
So repeatedly , until boolean The value of is false Time ,while End of cycle
*
Code demonstration :
//1) output 5 second " Action is the ladder of success ": int times = 0; //1) Initialization of loop variables while(times<5){ //2) Conditions of circulation
System.out.println(" Action is the ladder of success "); times++; //3) Change of cyclic variable } System.out.println(
" Continue execution ..."); /* Execution process :---- Band number times=0 true output times=1 true output times=2 true output
times=3 true output times=4 true output times=5 false while End of cycle Output continue ... */
//2) output 9 Multiplication table : int num = 1; //3*9=27 while(num<=9){ System.out.println(num+"*9="+
num*9); num++; //num+=2; } System.out.println(" Continue execution ...");
*
Guess the number game code :
public class Guessing { public static void main(String[] args) { Scanner scan =
new Scanner(System.in); int num = (int)(Math.random()*1000+1); //1 reach 1000 Random number within
System.out.println(num); // cheat //300( large ),200( Small ),250( yes ) System.out.println(" Guess !");
int guess = scan.nextInt(); //1. while(guess!=num){ //2. if(guess>num){ System.
out.println(" It's too big "); }else{ System.out.println(" It's too small "); } System.out.println(" Guess !"
); guess = scan.nextInt(); //3. } System.out.println(" Congratulations, you guessed right !"); } }
*
do…while structure : Execute before Judge , At least once
essential factor 1 And elements 3 Same time , be the first choice do…while
* grammar :
do{
Statement block
}while(boolean);
* Execution process :
Execute statement block first , Re judgment boolean Value of , if it is true be
Re execute statement block , Re judgment boolean Value of , if it is true be
Re execute statement block , Re judgment boolean Value of , if it is true be
Re execute statement block , So repeatedly , until boolean The value of is false, be do…while end
* Guess the number game code :public class Guessing { public static void main(String[] args) {
Scanner scan= new Scanner(System.in); int num = (int)(Math.random()*1000+1);
//1 reach 1000 Random number within System.out.println(num); // cheat // hypothesis num=250 //300( large ),200( Small ),250( yes )
int guess; do{ System.out.println(" Guess !"); guess = scan.nextInt(); //1+3 if(guess
>num){ System.out.println(" It's too big "); }else if(guess<num){ System.out.println(" It's too small ")
; }else{ System.out.println(" Congratulations, you guessed right "); } }while(guess!=num); //2 } }
<> Essence note :
*
Scanner Receive user input data : common 3 step , No need to understand , Recite it first
*
Branching structure :
* if…else if structure : Multiple roads
* switch…case structure : Multiple roads
advantage : efficient , Clear structure
shortcoming : Equality can only be judged for integers
break: Jump out switch
Common interview questions :switch What type of data can it act on
-------------------------byte,short,int,char,String, Enumeration type
*
loop : Execute the same or similar code repeatedly
*
Three elements of circulation :
* Initialization of loop variables
* Conditions of circulation ( Based on cyclic variables )
* Change of cyclic variable ( Towards the end of the cycle )
Cyclic variable : The number changed repeatedly throughout the cycle
*
Cyclic structure :
* while structure : Judgment before execution , It's possible not to do it once
* do…while structure : Execute before Judge , At least once
essential factor 1 And elements 3 Same time , be the first choice do…while
<> supplement :
*
Scope of variable / Range :
* Start with the declaration of the variable , Ends with the nearest brace containing it
*
Generate random number :
int num = (int)(Math.random()*1000+1); //1 reach 1000 Math.random()--------------0.0 reach
0.9999999999999999... *1000----------------------0.0 reach 999.99999999999999... +1--
-----------------------1.0 reach 1000.9999999999999... (int)----------------------1 reach
1000
*
Any complex program logic can be realized through three structures :
* Sequential structure : Line by line from top to bottom , Every sentence must go
* Branching structure : Conditionally execute a statement once , Not every sentence has to go
* Cyclic structure : Conditionally execute a statement multiple times , Not every sentence has to go
*
Tomorrow words :
1)for: in order to , A kind of cycle 2)continue: continue 3)result: result 4)answer: answer 5)array/arr: array 6)length: length
7)multi: many 8)table: form 9)addition: addition 10)index: subscript , Indexes 11)out of: go beyond 12)bounds: limit 13)
exception: abnormal
Technology