java 每日习题(十二) 题目:通过输入的第一个字母识别指定的几个单词

题目:通过输入的第一个字母识别周一至周日

1.  switch大小写使用同一case处理。

2.  通过标记使break和continue跳到指定位置,标记和while之间不能有其它代码。

package everyDay;

import java.util.Scanner;

import org.junit.Test;

public class RecWeeknum {

    @Test
    public void decideWeekNum() {
        System.out.println("please input weeknum start char,only one char");
        Scanner scan = new Scanner(System.in);
        String str = "";
        wil: while (true) {
            str = scan.next();
            if (str.length() == 1) {
                switch (str) {
                case "m":
                case "M":
                    System.out.println("monday");
                    break;

                case "t":
                case "T":
                    System.out.println("please input second char");
                    second: while (true) {
                        str = scan.next();
                        if (str.length() == 1) {
                            switch (str) {
                            case "u":
                            case "U":
                                System.out.println("tuesday");
                                break;
                            case "h":
                            case "H":
                                System.out.println("thursday");
                                continue second;

                            default:
                                System.out.println("please input again");
                                str = scan.next();
                                break;
                            }
                        } else {
                            System.out.println("only input one char");
                            continue;
                        }

                        break;

                    }
                    break;

                case "w":
                case "W":
                    System.out.println("wednesday");
                    break;

                case "f":
                case "F":
                    System.out.println("friday");
                    break;

                case "s":
                case "S":
                    System.out.println("please input second char");
                    second: while (true) {
                        str = scan.next();
                        if (str.length() == 1) {
                            switch (str) {
                            case "u":
                            case "U":
                                System.out.println("sunday");
                                break;
                            case "a":
                            case "A":
                                System.out.println("saturday");
                                break;

                            default:
                                System.out.println("please input again");
                                continue;
                            }
                        } else {
                            System.out.println("only input one char");
                            continue;
                        }

                        break;

                    }
                    break;

                default:
                    System.out.println("please input again");
                    continue wil;
                }
                break;

            } else {
                System.out.println("only input one char");
                
            }
        }

    }
}

 

转载于:https://www.cnblogs.com/mtlogs/p/5006494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值