当前位置:网站首页>Day05 branch and loop (II)
Day05 branch and loop (II)
2022-07-04 01:36:00 【ICErain0201】
1. local variable
Concept : Variables declared inside a method , You must assign a value before using .
describe | |
---|---|
Define the location | Must be defined in the method Location 1234 Can not be |
Scope of action | Within the braces closest to the current variable |
About duplicate names | In the overlapping scope of action , You can't have the same name |
Storage location | Basic data type , All stored on the stack (stack) in Reference data type , The name is stored on the stack (stack) in , Values are stored in the heap (heap) in |
Life cycle | As the method is stacked ( Pressing stack ) And take effect , As the method goes out of the stack ( Bomb stack ) And death |
package com.qfedu.test1;
/**
* local variable
* Concept : Variables declared inside a method , You must assign a value before using .
*
* Define the location : Must be defined in the method Location 1234 Can not be
* Scope of action : Within the braces closest to the current variable
* About duplicate names : In the overlapping scope of action , You can't have the same name
*
* Storage location :
* Basic data type , All stored on the stack (stack) in
* Reference data type , The name is stored on the stack (stack) in , Values are stored in the heap (heap) in
* Life cycle :
* As the method is stacked ( Pressing stack ) And take effect , As the method goes out of the stack ( Bomb stack ) And death
* @author WHD
*
*/
// Location 1
public class Test1 {
// Location 2
public static void main(String[] args) {
int d;
// System.out.println(d); Variable d No assignment Cannot access
int a = 10;
int b = 20;
if(a > b) {
System.out.println("a Greater than b Conditions established ");
// int a = 66;
int c = 66;
System.out.println(b);
}else {
// System.out.println(c); You can't visit c because c The scope of action of Only in if in
System.out.println(a);
int c = 66;
}
}
// Location 3
}
// Location 4
2. Variable name
The number of camels in the United States
word : Letter
Next : Underline
beautiful : Dollar symbol $
people : RMB symbol ¥
Count : Numbers
camel : Hump named
Variables can be named with letters , Underline , Dollar symbol , The RMB symbol begins , It can contain numbers , Cannot start with a number , Follow the hump nomenclature
studentNameAndAge
package com.qfedu.test1;
/**
* Variable name
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int a1 = 10;
int _a = 20;
int $c = 33;
int ¥d = 44;
// int 5d = 66; Cannot start with a number
String studentName = " Zhao si ";
}
}
3. loop
3.1 while loop
Counter initialization ;
while ( The loop condition ) {
Cyclic operationThe counter changes ;
}
while loop
while word : When ……
All loops must have four conditions
1. Counter initialization
2. Judge the condition
3. The loop body
4. The counter changes
package com.qfedu.test2;
/**
* while loop
* while : When ……
*
* All loops must have four conditions
* 1. Counter initialization
* 2. Judge the condition
* 3. The loop body
* 4. The counter changes
*
* demand : Use while Cycle to achieve 100 Study hard all the time
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int i = 1;
while(i <= 10000) {
System.out.println(" The first "+ i +" Study hard all the time ");
i++;
}
System.out.println(" Program end ~");
}
}
package com.qfedu.test2;
import java.util.Scanner;
/**
* The teacher checks whether Zhao Si's learning task is qualified every day , If it's not qualified ,
* I'm going to continue . The daily learning task assigned by the teacher to Zhao Si is :
* Read the textbook in the morning , Study the theory part , Programming in the afternoon , Master the code part
* @author WHD
*
*/
public class Test3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter whether your academic performance is qualified ?y/n");
String answer = input.next();
while(answer.equals("n")) {
System.out.println(" Read the textbook in the morning , Study the theory part ");
System.out.println(" Programming in the afternoon , Master the code part ");
System.out.println(" Please enter whether your academic performance is qualified ?y/n");
answer = input.next();
}
System.out.println(" congratulations , Complete the learning task ~");
}
}
3.2 do-while loop
do-while: do …… When
Counter initialization ;
do {
Cyclic operation
The counter changes ;
} while ( The loop condition );
while and do-while The difference between ?
while It's to judge first After execution Conditions not established Not once
do-while It's to execute first Post judgment Whether or not the conditions are valid At least once
package com.qfedu.test3;
import java.util.Scanner;
/**
* After a few days of study , The teacher gave Zhaosi a test question ,
* Let him write a program on the computer first , Then the teacher checks whether it is qualified .
* If it's not qualified , Continue to write ……
*
* while and do-while The difference between ?
* while It's to judge first After execution Conditions not established Not once
* do-while It's to execute first Post judgment Whether or not the conditions are valid At least once
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = "";
do {
System.out.println(" Write test code on the computer ");
System.out.println(" Please enter whether the result is qualified ?y/n");
answer = input.next();
}while(answer.equals("n"));
System.out.println(" Congratulations on completing the task !");
}
}
package com.qfedu.test3;
/**
* Use do-while Realization Print N Study hard all the time
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("do-while Loop to realize the "+ i +" Study hard all the time ");
i++;
}while(i < 0);
int j = 1;
while(j < 0) {
System.out.println("while Loop to realize the "+ j +" Study hard all the time ");
j++;
}
System.out.println(" Program end !");
}
}
3.3 for loop
for( Counter initialization ; Judge the condition ; The counter changes ){ The loop body ; }
for Circulation also requires the four essential parts of circulation
Counter initialization
Judge the condition
The loop body
The counter changes
Use for Cycle to achieve 100 Study printing well
for Loop execution order :
The first round
1. First perform counter initialization And only perform it once
2. Execute judgment conditions
3. Execution loop body
4. Execute counter change
The second round
Execute directly from the judgment condition
package com.qfedu.test4;
import java.util.Scanner;
/**
* for Loop implementation statistics 5 Results , And calculate the average score
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter a name ");
String name = input.next();
double sum = 0;
for(int i = 1;i <= 5;i++) {
System.out.println(" Please enter the first "+ i +" Results ");
double score = input.nextDouble();
sum = sum + score; // sum += score;
}
System.out.println(name + " Of 5 The average score of the course is " + sum / 5);
}
}
package com.qfedu.test5;
import java.util.Scanner;
/**
* Output addition table
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter a value ");
int num = input.nextInt();
for(int i = 0;i <= num;i++) {
System.out.println(i + "+" + (num - i) + "=" + num);
}
System.out.println(" Program end ~");
}
}
4. Cyclic comparison
difference :
1. The grammar is different
2. Execution order
while loop : First judge , Re execution
do-while loop : Execute first , To determine
for loop : First judge , Re execution
3. Application
The number of cycles is determined , Generally selected for loop
The number of cycles is uncertain , Generally selected while or do-while loop
5.break keyword
break keyword
break It can be used for switch in , To jump out of switch structure
It can also be used in loops , Means to jump out of the current loop , The number of incomplete executions No more execution
break Keyword in loop It is usually used in combination with branch structure
break After keyword Can't write code
package com.qfedu.test6;
/**
* break keyword
* break It can be used for switch in , To jump out of switch structure
* It can also be used in loops , Means to jump out of the current loop , The number of incomplete executions No more execution
*
* run 10 circle The first 8 circle Too tired sign out
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(" Running page "+ i +" circle ");
if(i == 8) {
System.out.println(" Too tired , Don't run away ");
break; // This represents the number of subsequent cycles that have not been completed No more execution Break the loop
}
}
System.out.println(" Program end ~");
}
}
package com.qfedu.test6;
/**
* while loop and do-while Use in loop break
* @author WHD
*
*/
public class Test3 {
public static void main(String[] args) {
int i = 1;
while(i <= 10) {
System.out.println(" Running page "+ i + " circle ");
if(i == 5) {
System.out.println(" Too tired , sign out ");
break;
}
i++;
}
System.out.println("===============================================");
int j = 1;
do {
System.out.println(" Running page "+ j +" circle ");
if(j == 6) {
System.out.println(" Too tired , rest ");
break;
}
j++;
}while(j <= 10);
}
}
package com.qfedu.test6;
import java.util.Scanner;
/**
* Cycle through a student 5 And calculate the average score ,
* If a score is entered as negative , Stop entry and prompt for entry errors
* @author WHD
*
*/
public class Test4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter a name ");
String name = input.next();
int sum = 0;
boolean flag = true; // Define Boolean variables It is used to judge whether farmers have entered wrong grades
for(int i = 1;i <= 5;i++) {
System.out.println(" Please enter the first "+ i + " Results ");
int score = input.nextInt();
if(score < 0) {
flag = false;
System.out.println(" Incorrect score entered , Please re-enter ");
break;
}
sum += score;
}
//
// if(flag) { // Boolean variables Write the variable name directly Equivalent to Variable == true
// System.out.println(" The average score is " + sum / 5);
// }else {
// System.out.println(" Incorrect score entry , No more averaging ");
// }
System.out.println(flag ? " The average score is " + sum / 5 : " Incorrect score entry , No more averaging " );
System.out.println(" Program end ");
}
}
Please have a look at Yaqing !
边栏推荐
- 求esp32C3板子连接mssql方法
- Technical practice online fault analysis and solutions (Part 1)
- Audio resource settings for U3D resource management
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
- mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- 2-redis architecture design to use scenarios - four deployment and operation modes (Part 2)
- 长文综述:大脑中的熵、自由能、对称性和动力学
- TP5 automatic registration hook mechanism hook extension, with a complete case
- About uintptr_ T and IntPtr_ T type
- CLP information - how does the digital transformation of credit business change from star to finger?
猜你喜欢
在寻求人类智能AI的过程中,Meta将赌注押向了自监督学习
Gee: create a new feature and set corresponding attributes
2020-12-02 SSM advanced integration Shang Silicon Valley
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
长文综述:大脑中的熵、自由能、对称性和动力学
Douban scoring applet Part-3
Future源码一观-JUC系列
Install the pit that the electron has stepped on
TP5 automatic registration hook mechanism hook extension, with a complete case
CesiumJS 2022^ 源码解读[8] - 资源封装与多线程
随机推荐
Force buckle day32
Future source code view -juc series
How to delete MySQL components using xshell7?
Software product download collection
I don't care about you. OKR or KPI, PPT is easy for you
Douban scoring applet Part-3
Mongodb learning notes: command line tools
中电资讯-信贷业务数字化转型如何从星空到指尖?
Future source code view -juc series
Stringutils and collectionutils
Pyrethroid pesticide intermediates - market status and future development trend
Gee: create a new feature and set corresponding attributes
“疫”起坚守 保障数据中台服务“不打烊”
All metal crowns - current market situation and future development trend
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
Future源码一观-JUC系列
Do you know the eight signs of a team becoming agile?
MySQL utilise la vue pour signaler les erreurs, Explicit / show ne peut pas être publié; Verrouillage des fichiers privés pour la table sous - jacente
我管你什么okr还是kpi,PPT轻松交给你
ThinkPHP uses redis to update database tables