验证输入是否满足条件

原文链接: http://www.cnblogs.com/xixixing/p/9769469.html

break跳出离它最近的循环。如while、for

验证输入的五个字母是否为大写。

public static void Main(string[] args)
        {
            Console.WriteLine("请输入五个大写字母:");
            while (true) {//死循环,直到正确输入
                bool a = true;
                string str = Console.ReadLine();//输入默认为字符串。若为int数据,则Convert.ToInt32(str);
                for (int i = 0; i < 5; i++) {
                    if (str[i] >= 'A' && str[i] <= 'Z') {//字符是以int数据存储的,字符等效数据
                        //空语句,无动作
                    } else {
                        a = false;
                        break;//一旦有小写字母,跳出for循环
                    }
                }
                if (a == true) {
                    Console.WriteLine("输入正确");
                    break;//跳出while循环
                } else {
                    Console.WriteLine("有小写字母,输入有误,请重新输入");
                }
            }
            Console.ReadKey();
        }

转载于:https://www.cnblogs.com/xixixing/p/9769469.html

猜你喜欢

转载自blog.csdn.net/weixin_30788731/article/details/94965599
今日推荐