当前位置:网站首页>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 !
边栏推荐
- 51 single chip microcomputer timer 2 is used as serial port
- Pesticide synergist - current market situation and future development trend
- Oracle database knowledge points that cannot be learned (II)
- “疫”起坚守 保障数据中台服务“不打烊”
- Pratique technique | analyse et solution des défaillances en ligne (Partie 1)
- PMP 考试常见工具与技术点总结
- mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
- MySQL - use of aggregate functions and group by groups
- How programmers find girlfriends through blind dates
猜你喜欢

【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client

In the process of seeking human intelligent AI, meta bet on self supervised learning

Gee: create a new feature and set corresponding attributes

Lightweight Pyramid Networks for Image Deraining

Douban scoring applet Part-3

Maximum likelihood method, likelihood function and log likelihood function

LeetCode 168. Detailed explanation of Excel list name

Since the "epidemic", we have adhered to the "no closing" of data middle office services

Conditional test, if, case conditional test statements of shell script

Function: store the strings entered in the main function in reverse order. For example, if you input the string "ABCDEFG", you should output "gfedcba".
随机推荐
Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
C import Xls data method summary I (upload files and create Workbooks)
基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
求esp32C3板子连接mssql方法
String hash, find the string hash value after deleting any character, double hash
Oracle database knowledge points (IV)
查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
In the process of seeking human intelligent AI, meta bet on self supervised learning
Human resource management online assignment
C import Xls data method summary V (complete code)
Introduction to superresolution
Future源码一观-JUC系列
Future source code view -juc series
MySQL introduction - functions (various function statistics, exercises, details, tables)
Idsia & supsi & usi | continuous control behavior learning and adaptive robot operation based on Reinforcement Learning
How programmers find girlfriends through blind dates
Install the pit that the electron has stepped on
Difference between value and placeholder
机器学习基础:用 Lasso 做特征选择
How to delete MySQL components using xshell7?