当前位置:网站首页>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
}
}
边栏推荐
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Deep understanding of P-R curve, ROC and AUC
- xss-labs-master靶场环境搭建与1-6关解题思路
- 字符串回文hash 模板题 O(1)判字符串是否回文
- Leetcode739 每日温度
- conda常用命令汇总
- Heap (priority queue)
- Post request body content cannot be retrieved repeatedly
- PyTorch搭建LSTM实现服装分类(FashionMNIST)
- Mish shake the new successor of the deep learning relu activation function
猜你喜欢

How to Easily Create Barplots with Error Bars in R

kubenetes中port、targetPort、nodePort、containerPort的区别与联系

Brush questions --- binary tree --2

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

还不会安装WSL 2?看这一篇文章就够了

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

堆(優先級隊列)

Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)

drools决策表的简单使用

PyTorch nn. Full analysis of RNN parameters
随机推荐
Map和Set
Differences between nodes and sharding in ES cluster
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
Heap (priority queue)
ThreadLocal的简单理解
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
浅谈sklearn中的数据预处理
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
MySQL与PostgreSQL抓取慢sql的方法
堆(优先级队列)
Jenkins user rights management
JZ63 股票的最大利润
mysql数据库基础
Addition, deletion, modification and query of MySQL table (Advanced)
How to Easily Create Barplots with Error Bars in R
drools执行完某个规则后终止别的规则执行
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
SCM power supply
Leetcode14 longest public prefix