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

2025-02-25 23:39:59
推荐回答(2个)
回答1:

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:

String testString =null;
while((testString=Console.readline())!="Q")
{
your code here
}

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