【JAVA】Java中switch的用法。

基础内容:设置num值然后选择输出。

package com.st;
public class Demo_11_13{
    public static void main(String[] args)
    {    
        int num=2;
        /*基本语法*/
        /*代码运行*/
        /*
         * switch匹配num变量的值,当num的值是1的时候
         * 程序就从case 1后开始运行
         * 开始运行!=运行这一行
        */

        switch (num) 
        {
        case 1:
            System.out.println("num变量的值是1");
        break;
        case 2:
            System.out.println("num变量的值是2");
        break;
        case 3:
            System.out.println("num变量的值是3");
        break;
        default:
            System.out.println("以上情况都不符合");
            break;
        }
}

//拓展内容1:去掉break如何执行。

num=4;
        switch(num) 
        {
        default:
            System.out.println("以上情况都不符合");
        case 2:
            System.out.println("num变量的值是2");
        case 1:
            System.out.println("num变量的值是1");
        case 3:
            System.out.println("num变量的值是3");
         }

 结果如上图,可见依然是寻找num 从第一个default执行然后顺序执行

拓展2: //一年12个月,对应四季,123月对应春季,以此类推,来打印num月份对应的是哪个季节

方法1:使用if+switch

public class Month 
{     
       
    public static void main(String[] args) 
    {
        
Scanner in=new Scanner(System.in);
        int i=in.nextInt(); 
 //此处为输入一个整数//
        int flag=0;//初始化一个flag
        if(i>=1&&i<=3)
            flag=1;
        if(i>=4&&i<=6)
            flag=2;
        if(i>=7&&i<=9)
            flag=3;
        if(i>=10&&i<=12)
            flag=4;
        switch (flag) {
        case 1:
            System.out.println("现在是春季");
            break;
        case 2:
            System.out.println("现在是夏季");
            break;
        case 3:
            System.out.println("现在是秋季");
            break;
        case 4:
            System.out.println("现在是冬季");
            break;
        default:
            System.out.println("输入错误");
            break;
        }    
    }
}

方法2:利用case顺序执行的特点直接空执行

 public class Month 
{     
    
    public static void main(String[] args) 
    {
        Scanner in=new Scanner(System.in);
        int month=in.nextInt();//利用case顺序执行的特点直接空执行
        switch (month) {
        case 1:
        case 2:
        case 3:
            System.out.println("现在是春季");
            break;
        case 4:
        case 5:
        case 6:
            System.out.println("现在是夏季");
            break;
        case 7:
        case 8:
        case 9:
            System.out.println("现在是秋季");
            break;
        case 10:
        case 11:
        case 12:
            System.out.println("现在是冬季");
            break;
        default:
            System.out.println("输入错误");
            break;
        }    
    }
}

//注意:switch和if...else...都可以完成程序分支
//但是switch只能匹配值,不能范围判断,但是if可以。

作业://在程序中输入一个整数变量,并赋予值,使用switch完成这个变量是奇数还是偶数。

package com.st;
import java.util.Scanner;
import com.sun.java_cup.internal.runtime.Scanner;
public class number 
{
  
 public static void main(String[] args)
  {
        Scanner number=new Scanner(System.in);
        int num=number.nextInt();
        switch (num%2)

{
        case 0:
            System.out.println("This is even number");
            break;
        case 1:
            System.out.println("This is uneven number");
            break;
        default:
            System.out.println("输入错误");
            break;
    }

      
  
}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值