当前位置:网站首页>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 !
边栏推荐
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- Ape anthropology topic 20 (the topic will be updated from time to time)
- Flink interview questions
- Personal decoration notes
- nacos簡易實現負載均衡
- Shell脚本-case in 和正则表达式
- Bird recognition app
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- 美团2022年机试
- Mysql8.0 learning record 17 -create table
猜你喜欢

【检测技术课案】简易数显电子秤的设计与制作

如何一站式高效管理固定资产?

Pain points and solutions of fixed assets management of group companies

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

钓鱼识别app

dsPIC30F6014a LCD 方块显示

nacos服务配置和持久化配置

Principle and application of single chip microcomputer timer, serial communication and interrupt system

3D打印Arduino 四轴飞行器

【pytorch】2.4 卷积函数 nn.conv2d
随机推荐
TV size and viewing distance
【电赛训练】红外光通信装置 2013年电赛真题
Summary of reptile knowledge points
[pytorch learning] torch device
An overview of the design of royalties and service fees of mainstream NFT market platforms
nacos服务配置和持久化配置
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Installing Oracle EE
Promise asynchronous programming
Mysql8.0 learning record 17 -create table
Jetson Nano 安装TensorFlow GPU及问题解决
2.4 activation function
FAQ | FAQ for building applications for large screen devices
Understanding and implementation of AVL tree
Shell script - definition, assignment and deletion of variables
Mysql 优化
【pytorch】nn.AdaptiveMaxPool2d
Embedded Engineer Interview Question 3 Hardware
I use flask to write the website "one"
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database