单分支结构:
只有一个条件,符合就执行。
public class Test{
public static void main(String[] args){
int num = (int)(Math.random()*10); //生成10以内随机整数
if(num>5){
System.out.println("大于五的数有:"+num); //输出大于五的数
}
}
}
双分支结构:
有两个条件,符合哪个就执行哪个语句块。
import java.util.Scanner;
public class Test{
public static void main(String[] args){
String a = "喜欢";
Scanner sc = new Scanner(System.in);
System.out.println("你喜欢我吗?");
String world = sc.nextLine();
if(world.equals(a)){
System/out/println("牛蛙牛蛙");
}else{
System.out.println("牛马");
}
}
}
多分支结构:
有多个条件,匹配上哪个就执行哪个语句块,如果都不匹配,就执行最后的else语句块。
public class Test{
public static void main(){
int num =(int)(Math.random()*100);
if(num <100 && num >50){
System.out.println("大于五十小于100");
}else if(num==50){
System.out.println("等于50");
}else{
System.out.println("小于五十大于100");
}
}
}
嵌套if分支结构:
分支结构中包含分支。
public class Test{
public static void main(){
int num =(int)(Math.random()*100);
if(num <100 && num >50){
if(num== 88){
Systen.out.println("随机数等于88");
}
}
}
switch多分支结构:
根据表达式值(具体的值)的不同执行许多不同的操作。
public class Test{
public static void main(String[] args){
char ch = 'a';
int num = (int)(Math.random() * 26);
char newCh = (char)(ch + num);
switch(newCh){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("元音"+ newCh);
break;
case 'y':
case 'w':
System.out.println("半元音"+ newCh);
break;
default:
System.out.println("辅音"+ newCh);
}