当前位置:网站首页>Select knowledge points of structure
Select knowledge points of structure
2022-07-06 05:06:00 【I have nothing to do and like to chase plays】
if Single choice structure
import java.util.Scanner;
public class IfDemo01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(" Please enter the content ");
String str= sc.nextLine();
//equals Determines whether the strings are equal
if(str.equals("hello")){
System.out.println(str);
}
System.out.println("END");
sc.close();
}
}
Please enter the content
hello
hello
END
if Double choice structure
1. grammar :
if( Boolean expression 1){
// If the expression 1 The value of is true Execute code
}elseif( Boolean expression 2){
// If the expression 2 The value of is true Execute code
}elseif( Boolean expression 3){
// If the expression 3 The value of is true Execute code
}else{
// If none of the above Boolean expressions are true Execute code
}
2. Case study
import java.util.Scanner;
public class IfDemo02 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int score=sc.nextInt();
if(score==100){
System.out.println(" Congratulations, full mark ");
}else if(score<100&&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(" fail, ");
}else{
System.out.println(" The results are illegal ");
}
}
3.if A statement can have at most one else sentence ,else Statements in else if After statement ,
if A statement can have several else if sentence , They have to be in else The statement before , Once one of them else if The statement is detected as true, Other else if as well as else Statement will skip execution .
Nested if structure :
1. grammar :
if( Boolean expression 1){ // If the Boolean expression 1 The value of is true Execute code if( Boolean expression 2){ // If the Boolean expression 2 The value of is true Execute code } }
switch Multiple choice structure
There is another way to implement the multi-choice structure switch case sentence .
switch case Statement to determine whether a variable is equal to a value in a series of values , Each value is called a branch
switch The variable type of the statement can be :
1.byte,short,int perhaps char.
2. from javase7 Start switch Support string Sting type .
3. meanwhile case Label must be a string constant or literal .
grammar :
switch(exoression){ case value: // sentence break;// Optional case value: // sentence break;// Optional // You can have any number of case sentence default:// Optional // sentence }
Case study :
public class SwitchDemo01 { public static void main(String[] args) { //case through char grade='F'; switch(grade){ case 'A': System.out.println(" good "); break; case 'B': System.out.println(" good "); break; case 'C': System.out.println(" pass "); break; case 'D': System.out.println(" Make persistent efforts "); break; case 'E': System.out.println(" Fail "); break; default: System.out.println(" Unknown level "); } } }
public class SwitchDemo02 { public static void main(String[] args) { String name=" Juen-Kai Wang "; switch(name){ case " Juen-Kai Wang ": System.out.println(" Wang Junkai is so handsome "); break; case " hanah ": System.out.println(" Wang Yuan is really handsome "); break; case " Jackson Yi ": System.out.println(" Yiyang Qianxi is really handsome. "); break; default: System.out.println("TFBOYS The root is MiaoHong , The development will be better and better "); } }}
边栏推荐
猜你喜欢
What are the advantages of the industry private network over the public network? What specific requirements can be met?
Nacos TC setup of highly available Seata (02)
Postman前置脚本-全局变量和环境变量
麦斯克电子IPO被终止:曾拟募资8亿 河南资产是股东
GAMES202-WebGL中shader的編譯和連接(了解向)
Programmers' position in the Internet industry | daily anecdotes
Rce code and Command Execution Vulnerability
Microblogging hot search stock selection strategy
Application of Flody
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
随机推荐
Acwing week 58
Orm-f & Q object
SQL injection vulnerability (MSSQL injection)
从0到1建设智能灰度数据体系:以vivo游戏中心为例
JS quick start (II)
Three methods of Oracle two table Association update
EditorUtility. The role and application of setdirty in untiy
Nestjs配置文件上传, 配置中间件以及管道的使用
What should the project manager do if there is something wrong with team collaboration?
[effective Objective-C] - memory management
Flink kakfa data read and write to Hudi
Yyds dry inventory SSH Remote Connection introduction
Weng Kai C language third week 3.1 punch in
Tetris
yolov5 tensorrt加速
Application of Flody
Pagoda configuration mongodb
GAMES202-WebGL中shader的編譯和連接(了解向)
Nacos - TC Construction of High available seata (02)
Zynq learning notes (3) - partial reconfiguration