【JAVA】自学笔记DAY1,Eclipse中switch的用法。

2021/12/6 22:18:46

本文主要是介绍【JAVA】自学笔记DAY1,Eclipse中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;
    }

      
  
}
}

 

 



这篇关于【JAVA】自学笔记DAY1,Eclipse中switch的用法。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程