m0_50986232 2021-06-26 15:49 采纳率: 84.6%
浏览 45
已采纳

C#控制台应用程序 (异常处理1)

 

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-06-26 21:01
    关注

      

    2)

    using System;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("请输入姓名:");
                string name = null;
                while ((name = Console.ReadLine()).Trim() == "") { Console.Write("姓名不能为空,请重新输入姓名:"); }
    
                Console.Write("请输入年龄(15-25岁之间):");
                int age;
                while (true)
                {
                    try
                    {
                        age = int.Parse(Console.ReadLine());
                        if (age >= 15 && age <= 25) break;
                        else Console.Write("年龄需要15-25岁,请重新输入年龄(15-25岁之间):");
                    }
                    catch
                    {
                        Console.Write("年龄为数字,请重新输入年龄(15-25岁之间):");
                    }
                }
                Console.Write("请输入email地址:");
                string email = null;
                while ((email = Console.ReadLine().Trim()).IndexOf("@") ==-1) { Console.Write("email地址错误,请重新输入email地址:"); };
    
                Console.WriteLine("输入的信息为:{0}\t{1}\t{2}", name, age, email);
                Console.ReadKey();
            }
        }
    }

    3)

    using System;
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = Console.ReadLine();
                int index = str.IndexOf("@");
                string name = str.Substring(0, index - 1);
                int dotIndex = str.IndexOf(".");
                string domain = str.Substring(index + 1, dotIndex - index) + str.Substring(dotIndex + 1);//增加获取com的代码
                Console.WriteLine(name);
                Console.WriteLine(domain);
    
    
                Console.ReadKey();
            }
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作