标题:C#如何实现下列功能
只看楼主
w464578029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-2-20
 问题点数:0 回复次数:5 
C#如何实现下列功能
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 面向对象版聊天机器人1
{
    class Program
    {
        static void Main(string[] args)
        {
            Robot r1 = new Robot();
            r1.Name = "小刀";
            r1.Eat(5);
            Robot r2 = new Robot();
            r2.Name = "大刀";
            r2.Eat(8);
            Console.WriteLine("请选择机器人:1-小刀,2-大刀");
            Robot r = new Robot();
            string str1 = Console.ReadLine();
            if (str1 == "1")
            {
                 r = r1;
            }
            else
            {
                 r = r2;
            }
            r.SayHello();
            while (true)
            {
                string str = Console.ReadLine();
                r.Speak(str);
            }
        }
    }
    class Robot
    {
        public string Name { get; set; }
        private int FullLevel { get; set; }
        public void SayHello()
        {
            Console.WriteLine("我叫{0}", Name);
        }
        public void Eat(int foodCount)
        {
            if (foodCount > 100)
            {
                Console.WriteLine("太饱了");
                return;
            }
            FullLevel = FullLevel + foodCount;

        }
        public void Speak(string str)
        {
            if (FullLevel <= 0)
            {
                Console.WriteLine("食物没了,饿死了,请喂我点吃的吧");
                
                return;
            }
            if (str.Contains("姓名") || str.Contains("名字"))
            {
                this.SayHello();
            }
            else if (str.Contains("女朋友"))
            {
                Console.WriteLine("年龄太小,不考虑!");
            }
            else
            {
                Console.WriteLine("听不懂");
            }
            FullLevel--;
        }
    }
}

这是我写的一个小东东,这有两个机器人,开始任选一个,第个机器人都有一个FullLevel值,一个初始值是5,一个是8,我想实现每说一句FullLevel值就减1,当FullLevel为0的时候就拒绝聊天,并提示输入一个数字,就像游戏里的加HP一样。前面的都实现了,这后面这个不知道怎么弄?
搜索更多相关主题的帖子: 机器人 
2011-02-20 23:38
yehuanfeng
Rank: 4
等 级:业余侠客
帖 子:75
专家分:201
注 册:2010-8-3
得分:0 
具体一点哦,哪个没有实现
“当FullLevel为0的时候就拒绝聊天,并提示输入一个数字” 这个功能你代码中没有涉及哦
2011-02-21 13:15
w464578029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-2-20
得分:0 
“当FullLevel为0的时候就拒绝聊天,并提示输入一个数字”这个不会实现,不知道怎么实现!
2011-02-22 00:23
libomvp529
Rank: 1
等 级:新手上路
帖 子:7
专家分:8
注 册:2009-7-14
得分:0 
fulllevel的初值是什么,你用了委托好像没得到值,
2011-02-22 11:37
清河房子
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-2-24
得分:0 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using

namespace ConsoleApplication3
{
   
    class Test
    {
        
           static void Main(string[] args)
        {
            bool mark=true ;
            Robot r1 = new Robot();
            r1.Name = "小刀";
            r1.Eat(5);
            Robot r2 = new Robot();
            r2.Name = "大刀";
            r2.Eat(8);
            Console.WriteLine("请选择机器人:1-小刀,2-大刀");
            Robot r = new Robot();
            string str1 = Console.ReadLine();
            if (str1 == "1")
            {
                 r = r1;
            }
            else
            {
                 r = r2;
            }
            r.SayHello();
            while (mark)
            {
                if (r.FullLevel <= 0)
                  {
                    Console.WriteLine("食物没了,饿死了,请喂我点吃的吧");
                    r.Eat(int .Parse (Console .ReadLine ()));
                  }
                else
                  {
                    string str = Console.ReadLine();
                    if (str == "退出")
                        mark = false;
                    r.Speak(str);
                  }
            }
               
        }
    }
    class Robot
    {
        public string Name ;
        public  int FullLevel = 3;
        public void SayHello()
        {
            Console.WriteLine("我叫{0}", Name);
        }
        public void Eat(int foodCount)
        {
            if (foodCount > 100)
            {
                Console.WriteLine("太饱了");
                return;
            }
            FullLevel = FullLevel + foodCount;

        }
        public void Speak(string str)
        {
           
            if (str.Contains("姓名") || str.Contains("名字"))
            {
                this.SayHello();
            }
            else if (str.Contains("你有女朋友吗?"))
            {
                Console.WriteLine("年龄太小,不考虑!");
            }
            
            else if (str=="退出")
                Console .WriteLine ("再见");
            else
            {
                Console.WriteLine("听不懂");
            }
            FullLevel--;
         }
        }
    }


2011-02-24 22:42
清河房子
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-2-24
得分:0 
我改改了,你们看看对不对
2011-02-24 22:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-332506-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.088012 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved