当前位置:网站首页>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 !
边栏推荐
- [pytorch] 2.4 convolution function nn conv2d
- JCL 和 SLF4J
- Shell script echo command escape character
- Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
- Interrupt sharing variables with other functions and protection of critical resources
- [pytorch] softmax function
- It technology ebook collection
- 【pytorch】nn. AdaptiveMaxPool2d
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
- Shell script - definition, assignment and deletion of variables
猜你喜欢

Principles of Microcomputer - internal and external structure of microprocessor

FAQ | FAQ for building applications for large screen devices

OSPF - virtual link details (including configuration commands)

Redis——Lettuce连接redis集群

树结构---二叉树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

3D printing Arduino four axis aircraft

Daily practice of C language - day 80: currency change

集团公司固定资产管理的痛点和解决方案

Principle and application of single chip microcomputer timer, serial communication and interrupt system
随机推荐
Daily office consumables management solution
The fixed assets management system enables enterprises to dynamically master assets
Football and basketball game score live broadcast platform source code /app development and construction project
How to manage fixed assets efficiently in one stop?
nacos簡易實現負載均衡
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
nacos服务配置和持久化配置
Shell脚本-while循环详解
Mysql8.0 learning record 17 -create table
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Shell script - special variables: shell $, $*, [email protected], $$$
Shell script case in and regular expressions
NoSQL数据库的安装和使用
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
树结构---二叉树2非递归遍历
[ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
集团公司固定资产管理的痛点和解决方案
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
3D打印Arduino 四轴飞行器
Simple load balancing with Nacos