程序有点长,请耐心看看,问题是怎样也跳不出循环
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ Robot1 r1 = new Robot1();
r1.Name = "鸡毛1号";
r1.Eat(3); Robot1 r2 = new Robot1();
r2.Name = "鸡毛2号";
r2.Eat(4);
Console.WriteLine("请选择机器人:1→鸡毛1号,2→鸡毛2号"); Robot1 r;
string str = Console.ReadLine();
if (str == "1")
{
r = r1;//r指向"r1指向的对象"
}
else
{
r = r2;
} r.SayHello();
while (true)
{
string str1 = Console.ReadLine();
r1.Speak(str1);
}
Console.ReadKey();
}
} class Robot1
{
public string _Name;
public string Name
{
set { _Name = value;}
get { return _Name; }
} private int _FullLevel;
private int FullLevel
{
set { _FullLevel = value; }
get { return _FullLevel; }
}
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("饿死了,不想说了,除非你喂我!!(如果喂食,请按“Y”,如果不喂食,请按“N”)");
string s = Console.ReadLine();
if (s == "Y")
{
Console.WriteLine("请输入你喂食的数量");
string food = Console.ReadLine();
int food1 = Convert.ToInt32(food);
if ((food1 >=10) || (food1 < 0))
{
Console.WriteLine("去你的");
return;
}
FullLevel = FullLevel + food1;
Console.WriteLine("继续");
}
else if (s == "N")
{
Console.WriteLine("不鸟你了");
return;
}
else
{
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.Text;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ Robot1 r1 = new Robot1();
r1.Name = "鸡毛1号";
r1.Eat(3); Robot1 r2 = new Robot1();
r2.Name = "鸡毛2号";
r2.Eat(4);
Console.WriteLine("请选择机器人:1→鸡毛1号,2→鸡毛2号"); Robot1 r;
string str = Console.ReadLine();
if (str == "1")
{
r = r1;//r指向"r1指向的对象"
}
else
{
r = r2;
} r.SayHello();
while (true)
{
string str1 = Console.ReadLine();
r1.Speak(str1);
}
Console.ReadKey();
}
} class Robot1
{
public string _Name;
public string Name
{
set { _Name = value;}
get { return _Name; }
} private int _FullLevel;
private int FullLevel
{
set { _FullLevel = value; }
get { return _FullLevel; }
}
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("饿死了,不想说了,除非你喂我!!(如果喂食,请按“Y”,如果不喂食,请按“N”)");
string s = Console.ReadLine();
if (s == "Y")
{
Console.WriteLine("请输入你喂食的数量");
string food = Console.ReadLine();
int food1 = Convert.ToInt32(food);
if ((food1 >=10) || (food1 < 0))
{
Console.WriteLine("去你的");
return;
}
FullLevel = FullLevel + food1;
Console.WriteLine("继续");
}
else if (s == "N")
{
Console.WriteLine("不鸟你了");
return;
}
else
{
Console.WriteLine("你究竟搞什么啊,艹");
}
} if(str.Contains("姓名")||str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("朋友"))
{
Console.WriteLine("秘密"); }
else
{
Console.WriteLine("你说什么?");
}
FullLevel--;
}
}
}
while (true)
{
string str1 = Console.ReadLine();
if (r1.Speak(str1) == false)
break;
} public bool Speak(string str)
{
if (FullLevel <= 0)
{
Console.WriteLine("饿死了,不想说了,除非你喂我!!(如果喂食,请按“Y”,如果不喂食,请按“N”)");
string s = Console.ReadLine();
if (s == "Y")
{
Console.WriteLine("请输入你喂食的数量");
string food = Console.ReadLine();
int food1 = Convert.ToInt32(food);
if ((food1 >= 10) || (food1 < 0))
{
Console.WriteLine("去你的");
return false;
}
FullLevel = FullLevel + food1;
Console.WriteLine("继续");
}
else if (s == "N")
{
Console.WriteLine("不鸟你了");
FullLevel--;
return false;
}
else
{
Console.WriteLine("你究竟搞什么啊,艹");
}
}
if (str.Contains("姓名") || str.Contains("名字"))
{
this.SayHello();
}
else if (str.Contains("朋友"))
{
Console.WriteLine("秘密");
}
else
{
Console.WriteLine("你说什么?");
}
FullLevel--;
return true;
}
{
this.SayHello(); FullLevel--;
return;
}
else if (str.Contains("朋友"))
{
Console.WriteLine("秘密"); FullLevel--;
return;
}
else
{
Console.WriteLine("你说什么?"); FullLevel--;
return;
}
//不要在外面FullLevel
退不出循环是因为你一直是while(true)
2楼正解
while(true)根本没办法跳出啊!!
不能这样呢