当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
HR wonderful dividing line
PyTorch nn.RNN 参数全解析
Map and set
Natural language processing series (II) -- building character level language model using RNN
Dynamic debugging of multi file program x32dbg
PyTorch nn. Full analysis of RNN parameters
CDA数据分析——AARRR增长模型的介绍、使用
XSS labs master shooting range environment construction and 1-6 problem solving ideas
机械臂速成小指南(七):机械臂位姿的描述方法
随机推荐
史上最易懂的f-string教程,收藏這一篇就够了
Test shift left and right
FastDateFormat为什么线程安全
Input a three digit number and output its single digit, ten digit and hundred digit.
JZ63 股票的最大利润
Leetcode122 the best time to buy and sell stocks II
Go学习笔记—基于Go的进程间通信
String palindrome hash template question o (1) judge whether the string is palindrome
SSH automatically disconnects (pretends to be dead) after a period of no operation
堆(优先级队列)
堆(優先級隊列)
SparkContext: Error initializing SparkContext解决方法
drools中then部分的写法
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
Leetcode14 longest public prefix
ORB-SLAM2不同线程间的数据共享与传递
YYGH-BUG-05
Log4j2
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
输入一个三位的数字,输出它的个位数,十位数、百位数。