java 流程控制语句

单分支结构:
        只有一个条件,符合就执行。

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);
		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shine-yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值