当前位置:网站首页>Day12 control flow if switch while do While guessing numbers game
Day12 control flow if switch while do While guessing numbers game
2022-07-02 12:11:00 【33 year old Java enthusiast】
Control process
Sequential structure
All programs are executed in sequence
if Statement selection structure
Single choice statement
if(a>0){System.out.println(“hello”);}
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter a number ");
score = scanner.nextInt();
if (score>40){
System.out.println(" This is larger than 40");
}
scanner.close();
}
}
Double choice statement
if(a>0){System.out.println(“hello”);}
else{System.out.println(“no”);};
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter a number ");
score = scanner.nextInt();
if (score>40){
System.out.println(" This is larger than 40");
}else {
System.out.println(" This is less than 40");
}
scanner.close();
}
}
Multi conditional statement
if(a>0){System.out.println(“hello”);}
else if(a>1){System.out.println(“no1”);}
else if(a>2){System.out.println(“no2”);}
else{System.out.println(“no3”);};
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01_text {
public static void main(String[] args) {
int score = 0;// Initialize grades
Scanner scanner =new Scanner(System.in); // introduce Scanner class
while (scanner.hasNextInt()){
// If the input is an integer, it will run all the time , Otherwise jump out of the loop
score = scanner.nextInt(); // Open input and assign
if (score>=90){
System.out.println("A level ");
}else if(score<90 && score>=80){
System.out.println("B level ");
}else if (score<80 && score>=70){
System.out.println("C level ");
}else if(score<70 && score>=60){
System.out.println("D level ");
}else if (score<60 && score>=0){
System.out.println(" The result is not up to standard ");
}else {
System.out.println(" The score entered is not legal ");
}}
scanner.close();// Turn off input
}
}
Switch sentence
Determine whether a variable is equal to a value in a series of values , Each value becomes a branch .
switch Supported variable types in statement :
- byte int short perhaps char
- from JAVA 7 Start switch Support string String Type .
- case The label must be a string or literal
switch(expression expression ){
case value Constant :{};break; // Optional
case value Constant :{};break;// Optional
default:{}// Optional
package com.ckw.blog.select;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
String str = "K";
Scanner scanner = new Scanner(System.in);
str = scanner.next();
switch (str){
case "A":// Matching character
System.out.println(" Good grades ");
break;// If not, it will appear case through
case "B":
System.out.println(" Average grade ");
break;
case "D":
System.out.println(" Average results ");
break;
case "E":
System.out.println(" Achievement deviation ");
break;
case "F":
System.out.println(" Failing in grades ");
break;
default:
System.out.println(" Unknown input ");
}
}
}
switch Be careful
- case The result is characters , There is no need for brackets
- case The result requires : Number .
- default Although it is optional , But still add as code integrity
Switch Follow if The difference between :
if Used to compare numbers , As an option
switch It is mainly used to match multiple character results , Make a choice
( The source code is still used if As a compiler )
switch Follow if Cooperate to output the score code
package com.ckw.blog.select;
import java.util.Scanner;
public class study01 {
public static void main(String[] args) {
int score = 0;// Initialize grades
String str = "k";// Initialization level
Scanner scanner =new Scanner(System.in); // introduce Scanner class
while (scanner.hasNextInt()){
// If the input is an integer, it will run all the time , Otherwise jump out of the loop
score = scanner.nextInt(); // Open input and assign
if (score>=90&&score<=100){
str = "A";
}else if(score<90 && score>=80){
str = "B";
}else if (score<80 && score>=70){
str = "C";
}else if(score<70 && score>=60){
str = "D";
}else if (score<60 && score>=0){
str = "E";
}else {
str = "F";;
}
switch (str){
case "A":// Matching character
System.out.println(" Good grades ");
break;// If not, it will appear case through
case "B":
System.out.println(" Average grade ");
break;
case "C":
System.out.println(" Average results ");
break;
case "D":
System.out.println(" Achievement deviation ");
break;
case "E":
System.out.println(" Failing in grades ");
break;
default:
System.out.println(" Unknown input ");
}}
scanner.close();// Turn off input
}
}
The game of guessing numbers
The first one is if structure .
- while
- if(){}else if(){}
- Relationship identifier
package com.ckw.blog.select;
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
int score = 0;
int nub = 40;
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter a number ");
while (score!=nub){
score = scanner.nextInt();
if(score>nub){
System.out.println(" big ");
}else if(score<nub){
System.out.println(" The small ");
}
}
System.out.println(" Congratulations, you're right ");
scanner.close();
}
}
Loop structure
while Loop statement
while( Boolean expression ){ result }
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
while(a<100){
System.out.println(a)};
}
}
do…while…
do{ Calculation formula }while( Boolean expression )
Execute once before entering the loop
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
do{
a++;System.out.println(a);}while(a>=1&&a<100);
// Execute the code once first , If the condition is turn, Then enter the cycle
}
}
do…while Follow while The difference between
while It's judgment before execution ,do…while It's execution before judgment
do…while The run will execute a cycle at least once
package com.ckw.blog.select;
public class while001 {
public static void main(String[] args) {
int a = 0;
while (a>0&&a<100){
System.out.println(" I'm the result 1 Of ");
System.out.println(a);
a++;
}
do{
a++;
System.out.println(" I'm the result 2 Of ");
System.out.println(a);}while (a>0&&a<100);
// Execute the code once first , If the condition is turn, Then enter the cycle
}
}
边栏推荐
- Jenkins用户权限管理
- drools决策表的简单使用
- Orb-slam2 data sharing and transmission between different threads
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- CDA data analysis -- Introduction and use of aarrr growth model
- Those logs in MySQL
- 二分刷题记录(洛谷题单)区间的甄别
- Brush questions --- binary tree --2
- Leetcode14 longest public prefix
- drools执行String规则或执行某个规则文件
猜你喜欢
drools动态增加、修改、删除规则
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
How does Premiere (PR) import the preset mogrt template?
GGPlot Examples Best Reference
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
Deep understanding of NN in pytorch Embedding
ES集群中节点与分片的区别
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Read the Flink source code and join Alibaba cloud Flink group..
随机推荐
Heap (priority queue)
Those logs in MySQL
MySQL indexes and transactions
倍增 LCA(最近公共祖先)
From scratch, develop a web office suite (3): mouse events
jenkins 凭证管理
Depth filter of SvO2 series
CONDA common command summary
lombok常用注解
Multiply LCA (nearest common ancestor)
YYGH-BUG-05
Addition, deletion, modification and query of MySQL table (Advanced)
ES集群中节点与分片的区别
(C language) octal conversion decimal
字符串回文hash 模板题 O(1)判字符串是否回文
Mish shake the new successor of the deep learning relu activation function
Maximum profit of jz63 shares
SVO2系列之深度濾波DepthFilter
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Time format display