当前位置:网站首页>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 !
边栏推荐
- Installing Oracle EE
- Common interview questions for embedded engineers 2-mcu_ STM32
- Interrupt sharing variables with other functions and protection of critical resources
- Jetson Nano 安装TensorFlow GPU及问题解决
- nacos服务配置和持久化配置
- Shell脚本-read命令:读取从键盘输入的数据
- 队列的实现和应用
- pcl_ Viewer command
- In the middle of the year, where should fixed asset management go?
- Class loading
猜你喜欢
随机推荐
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
NiO zero copy
Shell脚本-变量的定义、赋值和删除
Serialization, listening, custom annotation
Record a redis timeout
An overview of the design of royalties and service fees of mainstream NFT market platforms
如何高效拉齐团队认知
队列的实现和应用
Principles of Microcomputer - Introduction
Why is the Ltd independent station a Web3.0 website!
TV size and viewing distance
Embedded Engineer Interview Question 3 Hardware
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
dsPIC30F6014a LCD 方块显示
2.2 【pytorch】torchvision.transforms
Shell脚本-read命令:读取从键盘输入的数据
LogBack
pcl_ Viewer command
【检测技术课案】简易数显电子秤的设计与制作
Design and manufacture of simple digital display electronic scale