C#中让某个控制台程序在输入Q前一直执行下去的问题

namespace ConsoleApplication4
{
public class Travel
{
private int tourCost = 0;
private String tourLocation;
public int TourCost
{
get
{
if (tourLocation == "北京")
return tourCost = 1000;
else if (tourLocation == "上海")
return tourCost = 1500;
else if (tourLocation == "广州")
return tourCost = 2000;
else
return 0;
}
}
public String TourLocation
{
get
{
return tourLocation;
}
set
{
if (value != "北京" && value != "上海" && value != "广州")
tourLocation = null;
else
tourLocation = value;
}
}
class Program
{
static void Main(string[] args) //每次输入地点前都需要输入go 不知怎么解决
{
Console.WriteLine("请输入go开始程序,输入Q/q退出程序!");
String testString = Console.ReadLine();
while(testString!="Q"&&testString!="q")
{
Travel testobj = new Travel();
testobj.TourLocation =Console.ReadLine();
Console.WriteLine(testobj.TourCost);
Console.ReadLine();
}
}
}
}
}
每次输入地点前都需要输入一次go 怎样修改能够只输入一次(或者不输入)go就一直执行下去

第1个回答  2015-04-08
Console.WriteLine("请输入go开始程序,输入Q/q退出程序!");
do
{
if(Console.ReadLine().ToUpper().Equals("Q"))
break;
Travel testobj = new Travel();
testobj.TourLocation =Console.ReadLine();
Console.WriteLine(testobj.TourCost);
}while(!testString.ToUpper().Equals("Q"))
或者
Console.WriteLine("请输入go开始程序,输入Q/q退出程序!");
String testString = Console.ReadLine();
while(!testString.ToUpper().Equals("Q"))
{
Travel testobj = new Travel();
testobj.TourLocation =Console.ReadLine();
Console.WriteLine(testobj.TourCost);
testString = Console.ReadLine();
}追问

谢谢了

第2个回答  2015-04-08
String testString =null;
while((testString=Console.readline())!="Q")
{
your code here
}

while里赋值并同时判断是否是Q,要一起的,不能放外面。追问

谢了

本回答被提问者采纳
相似回答