当前位置:网站首页>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 !
边栏推荐
猜你喜欢

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

Redis -- lattice connects to redis cluster

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

猿人学第20题(题目会不定时更新)

I use flask to write the website "one"

Personal decoration notes

Principles of Microcomputer - internal and external structure of microprocessor

Daily practice of C language - day 80: currency change

Ape anthropology topic 20 (the topic will be updated from time to time)

3D printing Arduino four axis aircraft
随机推荐
Key points of NFT supervision and overseas policies
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Mysql 优化
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
Is it safe to dig up money and make new shares
How to launch circle of friends marketing and wechat group activities
树结构---二叉树2非递归遍历
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
[pytorch] softmax function
dsPIC30F6014a LCD 方块显示
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
Shell脚本-read命令:读取从键盘输入的数据
Log4j 日志框架
SDN_简单总结
Shell脚本-if else语句
如何一站式高效管理固定资产?
Redis——Lettuce连接redis集群
【检测技术课案】简易数显电子秤的设计与制作
Which method is good for the management of fixed assets of small and medium-sized enterprises?
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things