【问题标题】:How to use string, switch, case and bufferedreader?如何使用字符串、开关、大小写和缓冲阅读器?
【发布时间】:2018-02-20 02:56:51
【问题描述】:

我有疑问如何使用字符串、开关、大小写和缓冲读取器? 在我的情况下,输出应该是 ->

选择您的密码:

A.密码

B.12345

C.54321

然后,如果我选择“密码”,它就会出现 “您输入的密码是密码”。 然后,如果我输入了不在选择列表中的错误密码,外观将是 “错误!检查您的数据!”。 有人能回答吗?

【问题讨论】:

    标签: string switch-statement bufferedreader


    【解决方案1】:
        public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Choose your password:");
        System.out.println("A. Thepasskey");
        System.out.println("B. 12345");
        System.out.println("C. 54321");
        String line = br.readLine();
        switch (line) {
            case "A"://if one type A as choice
                System.out.println("Thepasskey");
                break;
            case "B":
                System.out.println("12345");
                break;
            case "C":
                System.out.println("54321");
                break;
            default:
                System.out.println("error! Check your data");
                break;
        }
    }
    

    【讨论】:

    • 那是我输入“thepasskey”作为密码时会出现的代码吗?不需要用“int”吗?
    • 我试过了,但系统在“case”和“string”中显示错误。
    • 在这里你可以输入你的选项为 A 或 B 或 C 所有这将是字符串而不是数字
    • 我的意思是当我输入“密码”这个词时,外观将是“您选择的密码是密码”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2014-02-22
    • 2023-03-28
    • 2015-04-10
    • 2015-08-30
    • 2020-06-30
    相关资源
    最近更新 更多