我在这里看不到问题。
当我调用一个条目时,我陷入无限循环。
我正在做wcf,所以我的服务器工作正常。这是我的客户。如果我把循环拿出来,一切都有效。但我需要这些菜单。所以我知道我的代码是正确的,但我的循环不是。
看不出我出错的地方。
class Client
{
static void Main(string[] args)
{
try
{
BikeReference.BikeSalesClient bikeRef = new BikeClient.BikeReference.BikeSalesClient();
String menu = "\n\nEnter:\n" +
"0 to get all the bike stock;\n" +
"1 to get all the bike types;\n" +
// ...
"8 to quit:\n";
Console.WriteLine(menu);
// will throw FormatException if not int
int entry = int.Parse(Console.ReadLine());
do
{
switch (entry)
{
case 0:
foreach (var obj in bikeRef.GetAllBikeStock())
{
Console.WriteLine("");
Console.WriteLine("Bike ID: {0}", obj.IdBikeStock);
Console.WriteLine("Bike Type ID: {0}", obj.IdBikeType);
// ...
Console.WriteLine("Sold: {0}", obj.isItSold);
//break;
}
break;
case 1:
Console.WriteLine(bikeRef.UpdateBikeStock(15));
break;
default:
Console.WriteLine("Unrecognised option...");
break;
}
}
while (entry != 7);
}
catch (Exception)//(Exception e)
{
// Console.WriteLine("{0} Exception caught.", e);
}
}
}
答案 0 :(得分:5)
在循环中更改您的入口变量
int entry = 0; //Init
do
{
Console.WriteLine(menu);
// will throw FormatException if not int
entry = int.Parse(Console.ReadLine());
...
} while (entry != 7);
答案 1 :(得分:4)
在循环内部,您不会修改变量“entry”。因此,除非您的第一个条目= 7,否则您将陷入循环,因为(entry != 7)
它始终是while (entry != 7);
在entry = int.Parse(Console.ReadLine());
之前添加行switch (entry)
这样你就可以继续询问“条目”,因为条目可能等于7.因此,退出循环。
int entry;
do
{
Console.WriteLine(menu);
entry = int.Parse(Console.ReadLine());
switch (entry)
//...
}while (entry != 7);
答案 2 :(得分:1)
如上所述,您必须更改循环内的entry
变量:
class Client
{
static void Main(string[] args)
{
try
{
BikeReference.BikeSalesClient bikeRef = new BikeClient.BikeReference.BikeSalesClient();
String menu = "\n\nEnter:\n" +
"0 to get all the bike stock;\n" +
"1 to get all the bike types;\n" +
// ...
"8 to quit:\n";
Console.WriteLine(menu);
// will throw FormatException if not int
int entry = int.Parse(Console.ReadLine());
do
{
switch (entry)
{
case 0:
foreach (var obj in bikeRef.GetAllBikeStock())
{
Console.WriteLine("");
Console.WriteLine("Bike ID: {0}", obj.IdBikeStock);
Console.WriteLine("Bike Type ID: {0}", obj.IdBikeType);
// ...
Console.WriteLine("Sold: {0}", obj.isItSold);
//break;
}
break;
case 1:
Console.WriteLine(bikeRef.UpdateBikeStock(15));
break;
default:
Console.WriteLine("Unrecognised option...");
break;
}
entry = int.Parse(Console.ReadLine());
}
while (entry != 7);
}
catch (Exception)//(Exception e)
{
// Console.WriteLine("{0} Exception caught.", e);
}
}
}
答案 3 :(得分:0)
切换后必须等待输入。否则该条目不能成为!= 7