当前位置:网站首页>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
}
}
边栏推荐
- Mish-撼动深度学习ReLU激活函数的新继任者
- 字符串回文hash 模板题 O(1)判字符串是否回文
- drools决策表的简单使用
- This article takes you to understand the operation of vim
- JZ63 股票的最大利润
- Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
- mysql表的增删改查(进阶)
- Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
- [untitled] how to mount a hard disk in armbian
- FastDateFormat为什么线程安全
猜你喜欢

Map和Set

深入理解PyTorch中的nn.Embedding

Heap (priority queue)

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

How to Easily Create Barplots with Error Bars in R

HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R

From scratch, develop a web office suite (3): mouse events

xss-labs-master靶场环境搭建与1-6关解题思路

Test shift left and right

PyTorch nn.RNN 参数全解析
随机推荐
还不会安装WSL 2?看这一篇文章就够了
JZ63 股票的最大利润
Take you ten days to easily finish the finale of go micro services (distributed transactions)
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
使用Sqoop把ADS层数据导出到MySQL
【工控老马】西门子PLC Siemens PLC TCP协议详解
Log4j2
arcgis js 4.x 地图中加入图片
mysql数据库基础
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Multiply LCA (nearest common ancestor)
How to Create a Beautiful Plots in R with Summary Statistics Labels
[C language] Yang Hui triangle, customize the number of lines of the triangle
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
This article takes you to understand the operation of vim
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
PyTorch中repeat、tile与repeat_interleave的区别
测试左移和右移