码迷,mamicode.com
首页 > 编程语言 > 详细

Java经典编程题50道之二十六

时间:2017-06-06 13:05:26      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:tchar   system   char   stat   source   首字母   can   ase   example   

请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

public class Example26 {
    public static void main(String[] args) {
        f();
    }

    public static void f() {
        System.out.println("请输入星期的第一个大写字母:");
        char ch = getChar();
        switch (ch) {
        case ‘M‘:
            System.out.println("该字母对应的是Monday。");break;
        case ‘W‘:
            System.out.println("该字母对应的是Wednesday。");break;
        case ‘F‘:
            System.out.println("该字母对应的是Friday。");break;
        case ‘T‘: {
            System.out.println("请输入星期的第二个字母:");
            char ch2 = getChar();
            if (ch2 == ‘U‘) {
                System.out.println("该字母对应的是Tuesday。");
            } else if (ch2 == ‘H‘) {
                System.out.println("该字母对应的是Thursday。");
            } else {
                System.out.println("无此写法!");
            }
        };break;
        case ‘S‘: {
            System.out.println("请输入星期的第二个字母:");
            char ch2 = getChar();
            if (ch2 == ‘U‘) {
                System.out.println("该字母对应的是Sunday");
            } else if (ch2 == ‘A‘) {
                System.out.println("该字母对应的是Saturday");
            } else {
                System.out.println("无此写法!");
            }
        };break;
        default:
            System.out.println("无法判断你输入的字符!!!");
        }
    }
    public static char getChar() {
        @SuppressWarnings("resource")
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        char ch = str.charAt(0);
        if (ch < ‘A‘ || ch > ‘Z‘) {
            System.out.println("首字母输入错误,请重新输入!!!");
            ch = getChar();
        }
        return ch;
    }
}

Java经典编程题50道之二十六

标签:tchar   system   char   stat   source   首字母   can   ase   example   

原文地址:http://www.cnblogs.com/qubo520/p/6950795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!