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();
            }
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化