为什么我按88会提示再见当结束不了程序
时间:2011-12-11
来源:互联网
{
机器人 p1 = new 机器人();
p1.Nane="小I";
p1.Eat(5);
机器人 p2 = new 机器人();
p2.Nane = "小J";
p2.Eat(3);
Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
string st3 = Console.ReadLine();
if (st3 == "1")
{
p1 = p2;
}
else
{
p2 = p1;
}
p1.SayHello();
while(true)
{
string st1=Console.ReadLine();
p1.Speak(st1);
}
}
}
class 机器人
{
public string Nane { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫{0}",Nane);
}
public void Eat(int foodConunt)
{
if(FullLevel>100)
{
return;
}
FullLevel = FullLevel + foodConunt;
}
public void Speak(string st)
{
if(FullLevel<=0)
{
Console.WriteLine("额死了 不聊了");
string srt1 = Console.ReadLine();
int food = Convert.ToInt32(srt1);
if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return;
}
}
if (st == "你好~~~")
{
Console.WriteLine("我今天心情不是很好别来烦我");
}
else if (st == "88")
{
Console.WriteLine("再见");
return;
}
else
{
Console.WriteLine("你说什么,我听不懂");
}
FullLevel--;
}
还有这段代码 if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return;
}
也结束不了 要怎么才能实现???
机器人 p1 = new 机器人();
p1.Nane="小I";
p1.Eat(5);
机器人 p2 = new 机器人();
p2.Nane = "小J";
p2.Eat(3);
Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
string st3 = Console.ReadLine();
if (st3 == "1")
{
p1 = p2;
}
else
{
p2 = p1;
}
p1.SayHello();
while(true)
{
string st1=Console.ReadLine();
p1.Speak(st1);
}
}
}
class 机器人
{
public string Nane { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫{0}",Nane);
}
public void Eat(int foodConunt)
{
if(FullLevel>100)
{
return;
}
FullLevel = FullLevel + foodConunt;
}
public void Speak(string st)
{
if(FullLevel<=0)
{
Console.WriteLine("额死了 不聊了");
string srt1 = Console.ReadLine();
int food = Convert.ToInt32(srt1);
if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return;
}
}
if (st == "你好~~~")
{
Console.WriteLine("我今天心情不是很好别来烦我");
}
else if (st == "88")
{
Console.WriteLine("再见");
return;
}
else
{
Console.WriteLine("你说什么,我听不懂");
}
FullLevel--;
}
还有这段代码 if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return;
}
也结束不了 要怎么才能实现???
作者: wuliohuond 发布时间: 2011-12-11
你Speak函数虽然return了,但是你while循环没结束,当然退步了程序了
把你Speak函数改造一下就可以了
C# code
把你Speak函数改造一下就可以了
C# code
static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { 机器人 p1 = new 机器人(); p1.Nane = "小I"; p1.Eat(5); 机器人 p2 = new 机器人(); p2.Nane = "小J"; p2.Eat(3); Console.WriteLine("请选择机器人:1或2,1是小I,2是小J"); string st3 = Console.ReadLine(); if (st3 == "1") { p1 = p2; } else { p2 = p1; } p1.SayHello(); while (true) { string st1 = Console.ReadLine(); if (p1.Speak(st1) == true) //Speak函数返回true了,结束程序 break; } } } class 机器人 { public string Nane { get; set; } private int FullLevel { get; set; } public void SayHello() { Console.WriteLine("我叫{0}", Nane); } public void Eat(int foodConunt) { if (FullLevel > 100) { return; } FullLevel = FullLevel + foodConunt; } public bool Speak(string st) { if (FullLevel <= 0) { Console.WriteLine("额死了 不聊了"); string srt1 = Console.ReadLine(); int food = Convert.ToInt32(srt1); if (food <= 0) { Console.WriteLine("兄弟你玩我的吧"); return true; } } if (st == "你好~~~") { Console.WriteLine("我今天心情不是很好别来烦我"); } else if (st == "88") { Console.WriteLine("再见"); return true; //通知while,结束 } else { Console.WriteLine("你说什么,我听不懂"); } FullLevel--; return false; } }
作者: gxingmin 发布时间: 2011-12-11
机器人 p1 = new 机器人();
p1.Nane="小I";
p1.Eat(5);
机器人 p2 = new 机器人();
p2.Nane = "小J";
p2.Eat(3);
Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
string st3 = Console.ReadLine();
if (st3 == "1")
{
p1 = p2;
}
else
{
p2 = p1;
}
p1.SayHello();
while(true)
{
string st1=Console.ReadLine();
if (p1.Speak(st1) == true) //Speak函数返回true了,结束程序
break;
}
}
}
class 机器人
{
public string Nane { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫{0}",Nane);
}
public void Eat(int foodConunt)
{
if(FullLevel>100)
{
return;
}
FullLevel = FullLevel + foodConunt;
}
public void Speak(string st)
{
if(FullLevel<=0)
{
Console.WriteLine("额死了 不聊了");
string srt1 = Console.ReadLine();
int food = Convert.ToInt32(srt1);
if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return true;
}
}
if (st == "你好~~~")
{
Console.WriteLine("我今天心情不是很好别来烦我");
}
else if (st == "88")
{
Console.WriteLine("再见");
return true;
}
else
{
Console.WriteLine("你说什么,我听不懂");
}
FullLevel--;
return false;
为什么我修改后提示
错误 1 运算符“==”无法应用于“void”和“bool”类型的操作数
错误 2 由于“机器人2.机器人.Speak(string)”返回 void,返回关键字后面不得有对象表达式
p1.Nane="小I";
p1.Eat(5);
机器人 p2 = new 机器人();
p2.Nane = "小J";
p2.Eat(3);
Console.WriteLine("请选择机器人:1或2,1是小I,2是小J");
string st3 = Console.ReadLine();
if (st3 == "1")
{
p1 = p2;
}
else
{
p2 = p1;
}
p1.SayHello();
while(true)
{
string st1=Console.ReadLine();
if (p1.Speak(st1) == true) //Speak函数返回true了,结束程序
break;
}
}
}
class 机器人
{
public string Nane { get; set; }
private int FullLevel { get; set; }
public void SayHello()
{
Console.WriteLine("我叫{0}",Nane);
}
public void Eat(int foodConunt)
{
if(FullLevel>100)
{
return;
}
FullLevel = FullLevel + foodConunt;
}
public void Speak(string st)
{
if(FullLevel<=0)
{
Console.WriteLine("额死了 不聊了");
string srt1 = Console.ReadLine();
int food = Convert.ToInt32(srt1);
if(food<=0)
{
Console.WriteLine("兄弟你玩我的吧");
return true;
}
}
if (st == "你好~~~")
{
Console.WriteLine("我今天心情不是很好别来烦我");
}
else if (st == "88")
{
Console.WriteLine("再见");
return true;
}
else
{
Console.WriteLine("你说什么,我听不懂");
}
FullLevel--;
return false;
为什么我修改后提示
错误 1 运算符“==”无法应用于“void”和“bool”类型的操作数
错误 2 由于“机器人2.机器人.Speak(string)”返回 void,返回关键字后面不得有对象表达式
作者: wuliohuond 发布时间: 2011-12-13
把public void SayHello()
改成
public bool SayHello()
把return;
改成 return true;
改成
public bool SayHello()
把return;
改成 return true;
作者: gxingmin 发布时间: 2011-12-13
晕了晕了
上面看错了
是把 public void Speak(string st)
改成
public bool Speak(string st)
上面看错了
是把 public void Speak(string st)
改成
public bool Speak(string st)
作者: gxingmin 发布时间: 2011-12-13
请问
为什么要改成bool呢 麻烦你解释一下
为什么要改成bool呢 麻烦你解释一下
作者: wuliohuond 发布时间: 2011-12-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28