Java语言中switch如何对字符串(String)类型实现一个选择的功能呢,要有个具体的代码才能理解

Java语言中switch如何对字符串(String)类型实现一个选择的功能呢,要有个具体的代码才能理解

问题描述:

Java语言中switch如何对字符串(String)类型实现一个选择的功能呢,要有个具体的代码才能理解

正如楼上的代码那样,String直接就可以实现switch的选择分支,写在case条件中哦

for example

public class Sample {
    public static void main(String[] args) {
        for (int i=0; i<5; i++) {
            String s = String.format("%d", i);
            switch(s) {
            case "0":
                System.out.println("0");
                break;
            case "1":
                System.out.println("1");
                break;
            default:
                System.out.println("others");
                break;
            }
        }
    } 
}

Scaner result=new Scanner(System.in)
int num;
if( (result-'0')>=0||(result-'0')<=9)
    num=1
else
   num=0
 switch(num)
 {
 case  "1" :
      System.out.println("是数字");
             break;
    default:
        System.out.println("other");
            break;

 }