110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 面向对象版聊天机器人
{
class Program
{
static void Main(string[] args)
{
机器人 K1 = new 机器人();
K1.Name = "小K";
K1.Eat(5);
K1.SayHello();
while (true)
{
string str = Console.ReadLine();
K1.Speak(str);
}
Console.ReadKey();
}
}
class 机器人
{
public string Name { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫:{0}", Name);
}
public void Eat(int foodCount)
{
if (FullLevel > 100)
{
return;
}
FullLevel = FullLevel + foodCount;
}
public void Speak(string str)
{
if (FullLevel <= 0)
{
Console.WriteLine("饿死了,不说了");
}
if (str.Contains("姓名") || str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!");
}
else
{
Console.WriteLine("听不懂");
}
FullLevel--;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 面向对象版聊天机器人
{
class Program
{
static void Main(string[] args)
{
机器人 K1 = new 机器人();
K1.Name = "小K";
K1.Eat(5);
K1.SayHello();
while (true)
{
string str = Console.ReadLine();
if(K1.Speak(str)<0){
break;
}
}
Console.ReadKey();
}
}
class 机器人
{
public string Name { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫:{0}", Name);
}
public void Eat(int foodCount)
{
if (FullLevel > 100)
{
return;
}
FullLevel = FullLevel + foodCount;
}
public int Speak(string str)
{
if (FullLevel <= 0)
{
Console.WriteLine("饿死了,不说了");
return -1;
}
if (str.Contains("姓名") || str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!");
}
else
{
Console.WriteLine("听不懂");
}
FullLevel--;
return FullLevel;
}
}
}
//简单的修改了一下
class Program
{
static void Main(string[] args)
{
机器人 K1 = new 机器人();
K1.Name = "小K";
K1.Eat(5);
K1.SayHello();
while (机器人.FullLevel>=0)
{
string str = Console.ReadLine();
K1.Speak(str);
}
Console.ReadKey();
}
}
class 机器人
{
public string Name { get; set; }
public static int FullLevel;
public void SayHello()
{
Console.WriteLine("你好,我叫:{0},有什么想问的吗?", Name);
Console.WriteLine("");
}
public void Eat(int foodCount)
{
if (FullLevel > 100)
{
return;
}
FullLevel = FullLevel + foodCount;
}
public void Speak(string str)
{
if (FullLevel <= 0)
{
Console.WriteLine("饿死了,不说了");
}
else
{
if (str.Contains("姓名") || str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!");
Console.WriteLine("");
}
else
{
Console.WriteLine("听不懂");
Console.WriteLine("");
}
}
FullLevel--;
}
}
这个地方改一下:
while (机器人.FullLevel>=0)
{
string str = Console.ReadLine();
K1.Speak(str);
}
这个地方改一下,试试
public static int FullLevel;