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

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

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;
    }
}

时间: 2024-08-11 07:47:14

Java经典编程题50道之二十六的相关文章

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

一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Example25 {    public static void main(String[] args) {        f2(123454321);    }//方法一    public static void f1(int n) {        if (n >= 10000 && n < 100000) {            String s = S

Java经典编程题50道之二十

有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. public class Example20 {    public static void main(String[] args) {        sum(20);    } public static void sum(int n) {        double x = 2.0;        double y = 1.0;        double t;        double

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

求一个3*3矩阵对角线元素之和. public class Example29 {    public static void main(String[] args) {        int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };        sum(a);    } public static void sum(int[][] a) {        System.out.println("您输入的3*3矩阵是:");

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

对10个数进行排序. public class Example28 {    public static void main(String[] args) {        int[] s = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };        BubbleSort(s);    } public static void BubbleSort(int[] a) {        System.out.print("原数组为:");        for (int

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

利用递归方法求5!. public class Example22 {    public static void main(String[] args) {        int n = 5;        long s = sum(n);        System.out.println(n + "!= " + s);    } public static long sum(int n) {        long s = 1;        if (n == 1||n==0)

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

有5个人坐在一起,问第5个人多少岁,他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁. 问第三个人,他说比第2人大两岁.问第2个人, 说比第一个人大两岁.最后问第一个人,他说是10岁. 请问第五个人多大? public class Example23 {    public static void main(String[] args) {        age();    } public static void age() {        int age = 10;      

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

求100之内的素数. public class Example27 {    public static void main(String[] args) {        prime();    } public static void prime() {        System.out.print(2 + "\t");        System.out.print(3 + "\t");        int count = 2;        boolea

Java经典编程题50道之二

判断101-200之间有多少个素数,并输出所有素数. public class Example02 {    public static void main(String[] args) {        prime();    } public static void prime() {        int count = 0;        for (int i = 101; i < 2000; i += 2) {            boolean flag = false;     

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

给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. public class Example24 {    public static void main(String[] args) {        f(123789);    } public static void f(long l) {        String s = Long.toString(l);        char[] c = s.toCharArray();        int j = c.len