当前位置:网站首页>Day06 branch structure and cycle (III)
Day06 branch structure and cycle (III)
2022-07-01 09:12:00 【ICErain0201】
1.continue keyword
continue : continue
Applicable scenario : It can only be used in a cycle
effect : Jump out of this cycle , Continue with next cycle
break and continue The difference between ?
Different scenarios ,break It can be used for switch And the cycle ,continue It can only be used in a cycle
The effect is different :
break Indicates an interrupt loop , The number of cycles that have not been executed is no longer executed
continue Jump out of this cycle , Continue with next cycle
package com.qfedu.test1;
/**
* continue : continue
* Applicable scenario : It can only be used in a cycle
* effect : Jump out of this cycle , Continue with next cycle
*
* break and continue The difference between ?
* Different scenarios ,break It can be used for switch And the cycle ,continue It can only be used in a cycle
* The effect is different :
* break Indicates an interrupt loop , The number of cycles that have not been executed is no longer executed
* continue Jump out of this cycle , Continue with next cycle
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
// apply for Loop printing 1 ~ 10 When i The values for 5 Separate use break and continue See the effect
for (int i = 1; i <= 10; i++) {
if(i == 5) {
continue;
}
System.out.println(i);
}
}
}
package com.qfedu.test1;
import java.util.Scanner;
/**
* Cycle input Java Students' grades in class , The statistical score is greater than or equal to 80 The proportion of students who get a score
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter the number of people ");
int num = input.nextInt();
double count = 0;
for(int i = 1;i <= num;i++) {
System.out.println(" Please enter the first "+ i +" Personal achievements ");
int score = input.nextInt();
if(score < 80) {
System.out.println(" The score is less than 80 branch , There is no need to accumulate ");
continue;
}
count++;// The code can be executed to this step Indicates that the score is greater than or equal to 80 Points of Because if it's less than 80 branch Will perform continue continue Subsequent code No more execution
}
System.out.println(" Greater than or equal to 80 The proportion of the number of points is " + count / num * 100 + "%");
}
}
package com.qfedu.test1;
/**
* while Circulation and do-while Use in loop continue
* @author WHD
*
*/
public class Test3 {
public static void main(String[] args) {
int i = 0;
while(i <= 10) {
i++;
if(i == 5) {
continue;
}
System.out.println(i);
}
System.out.println("====================================");
int j = 0;
do {
j++;
if(j == 5) {
continue;
}
System.out.println(j);
}while(j <= 10);
}
}
package com.qfedu.test1;
/**
* seek 1~10 Between all even numbers and
* @author WHD
*
*/
public class Test4 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if(i % 2 != 0) {
continue;
}
sum += i;
}
System.out.println(sum);
System.out.println("===========================================");
int sum1 = 0;
for(int i = 0;i <= 10;i+=2) {
sum1 += i;
}
System.out.println(sum1);
}
}
3. Double cycle
The outer variable changes once The inner circulation variable changes one round
The outer loop controls the number of lines
The number of inner loop control columns
The number of elements in the first line determines the initial value of the counter ,
More and more elements , Just ++, When counter ++ When , We have to set an upper limit , That is, our condition must be less than or equal to a certain value ,
Otherwise it's a dead cycle
Fewer and fewer elements , Just --, When counter -- When , We have to set a lower limit , That is, our condition must be greater than or equal to a certain value ,
Otherwise it's a dead cycle
package com.qfedu.test3;
/**
* parallelogram
* When we use multiple loops to print triangles :
* The number of elements in the first line determines the initial value of the counter ,
* More and more elements , Just ++, When counter ++ When , We have to set an upper limit , That is, our condition must be less than or equal to a certain value ,
* Otherwise it's a dead cycle
* Fewer and fewer elements , Just --, When counter -- When , We have to set a lower limit , That is, our condition must be greater than or equal to a certain value ,
* Otherwise it's a dead cycle
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { // Number of lines 5 That's ok perform 5 The second outer cycle
// The left half Inverted triangle
for(int j = 5;j >= i;j--) {
System.out.print("&");
}
// The right half rectangular
for(int j = 1;j <= 5;j++) {
System.out.print("*");
}
// Line break
System.out.println();
}
}
}
package com.qfedu.test4;
/**
* Print equilateral triangles using multiple loops
*
* The outer loop controls the number of lines
* The number of inner loop control columns
*
* The number of elements in the first line determines the initial value of the counter ,
* More and more elements , Just ++, When counter ++ When , We have to set an upper limit , That is, our condition must be less than or equal to a certain value ,
* Otherwise it's a dead cycle
* Fewer and fewer elements , Just --, When counter -- When , We have to set a lower limit , That is, our condition must be greater than or equal to a certain value ,
* Otherwise it's a dead cycle
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) { // 5 Row pattern
// The left half Inverted triangle
for(int j = 5;j >= i ;j--) {
System.out.print("^");
}
// The right half right triangle Because the shape of the left half Squeeze the right side into an equilateral triangle
for(int j = 1;j <= i * 2 -1 ;j++) {
System.out.print("*");
}
// Line break
System.out.println();
}
}
}
package com.qfedu.test4;
import java.util.Scanner;
/**
* Use the multi loop print bank menu system
* @author WHD
*
*/
public class Test4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice = 0;
do {
System.out.println("***********************************************");
System.out.println("************* Welcome to use ATM Bank menu system *************");
System.out.println("1. Accounts 2. deposit 3. Withdraw money 4. The loan 5. Change Password 6. Check the balance 0. sign out ");
System.out.println("***********************************************");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println(" Perform account opening function ");
break;
case 2:
System.out.println(" Execute the deposit function ");
break;
case 3:
System.out.println(" Perform the withdrawal function ");
break;
case 4:
System.out.println(" Perform the loan function ");
break;
case 5:
System.out.println(" Perform the password modification function ");
break;
case 6:
System.out.println(" Execute the function of querying balance ");
break;
case 0:
System.out.println(" sign out , Welcome to use next time ~");
break;
default:
System.out.println(" Incorrect input , Please re-enter ");
break;
}
} while (choice != 0);
System.out.println(" Program end ");
}
}
Please have a look at Yaqing !
边栏推荐
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
- Mysql8.0 learning record 17 -create table
- How to manage fixed assets efficiently in one stop?
- Daily office consumables management solution
- 类加载
- [video game training] real topic of 2013 video game of infrared optical communication device
- Meituan machine test in 2022
- 如何一站式高效管理固定资产?
- 易点易动助力企业设备高效管理,提升设备利用率
猜你喜欢
【pytorch】2.4 卷积函数 nn.conv2d
nacos简易实现负载均衡
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
如何解决固定资产管理和盘点的难题?
Redis——Lettuce连接redis集群
【pytorch】softmax函数
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
【电赛训练】红外光通信装置 2013年电赛真题
Microcomputer principle - bus and its formation
如何做好固定资产管理?易点易动提供智能化方案
随机推荐
队列的实现和应用
Shell脚本-数组定义以及获取数组元素
树结构---二叉树2非递归遍历
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
How to launch circle of friends marketing and wechat group activities
Shell脚本-for循环和for int循环
Redis -- lattice connects to redis cluster
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
An overview of the design of royalties and service fees of mainstream NFT market platforms
【pytorch】nn.AdaptiveMaxPool2d
Record a redis timeout
Promise asynchronous programming
How to solve the problem of fixed assets management and inventory?
如何高效拉齐团队认知
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
Shell script -for loop and for int loop
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
Naoqi robot summary 28