• 一个控制台程序,模拟机器人对话


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace RobotTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                机器人 robot = new 机器人();
                robot.Name = "小艾";
                robot.Eat(5);
                robot.SayHello();
                while(true)
                {
                    string qu = Console.ReadLine();
                    robot.Speak(qu);
                }
                Console.ReadKey();
            }
        }
        class 机器人
        {
            public string Name { get; set; }
            private int FullLeve { get; set; }
            public void SayHello()
            {
                Console.WriteLine("您好,我是{0}",this.Name);
            }
            public void Eat(int foods)
            {
                if (FullLeve > 100 )
                {
                    Console.WriteLine("撑死了,不能吃了!");
                    return;
                }
                FullLeve += foods;
            }
            int num = 0; 
            public void Speak(string Question)
            
            {
                
                if (FullLeve <= 0)
                {
                    if (num == 0)
                    {
                        Console.WriteLine("饿死了,赶紧为我点吃的吧,我只能吃数字^-^"); num ++;return;
                    }
                   // var Cusfood =  Console.ReadLine();
                    int number = 0; 
                    if (!int.TryParse(Question, out number) || Question.IndexOf("-") >= 0) 
                    {
                        Console.WriteLine("不带这么玩的,喂我点能吃的东西行吗,亲");
                        num++;
                        return;
                    }
                    else
                    {
                        FullLeve += Convert.ToInt32(Question);
                        Console.WriteLine("谢谢亲,我吃饱了!可以继续愉快的玩耍了!");
                        return;
                    }
                    
                }
                if (Question.Contains("姓名") || Question.Contains("名字"))
                {
                    this.SayHello();
                }
                else if (Question.Contains("女朋友"))
                {
                    Console.WriteLine("年龄小,不考虑!");
                }
                else
                {
                    Console.WriteLine("听不懂!");
                }
                FullLeve--;
            }
            public bool ValidateNum(string no)
            {
                Regex rg = new Regex("^\d+$"); return true;
              //  Match mt = rg.Matches(no);
            }
        }
    }
  • 相关阅读:
    Servlet编程实例 续2
    Servlet编程实例 续1
    Servlet编程实例
    Servlet包介绍
    Servlet处理流程分析
    Servlet简介
    初始String
    类对象的定义
    类和对象
    JDBC编程之程序优化
  • 原文地址:https://www.cnblogs.com/zix1314/p/5554890.html
Copyright © 2020-2023  润新知