switch语句用法

import java.io.PrintStream;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws java.io.UnsupportedEncodingException {
        PrintStream ps = new PrintStream(System.out, true, "UTF-8");
        Scanner in = new Scanner(System.in, "UTF-8");
        String str=in.next();
        String result;
        switch (str) {
            case "获取某一个位置的字符":
                result = "charAt";
            case "判断字符串内容是否相同":
                result ="equal";
            case "比较大小":
                result="compareTo";
            case "得到子串":
                result="substring";
            case "寻找字符":
                result="indexof";
            case "判断是否以xxx开始":
                result="startsWith";
            case "全部转成大写":
                result="toUpperCase";
            case "全部转成小写":
                result="toLowerCase";
            case "获得长度":
                result="length";
            default:
                result = "你输入是不是搞错了?";
                break;
        }
        ps.println(result);
    }
}

2 条评论

  • 1