简体   繁体   English

陷入无限循环

[英]Stuck in infinite loop

I cant see the problem here.我看不到这里的问题。

I am stuck in an infinite loop when I call an entry.当我调用一个条目时,我陷入了无限循环。

I am doing wcf so I have my server working fine.我正在做 wcf,所以我的服务器工作正常。 This is my client.这是我的客户。 Everything works if i take the loops out.如果我取出循环,一切都会正常。 But I need these for the menu.但我需要这些作为菜单。 So I know my code is right but my loops aren't.所以我知道我的代码是正确的,但我的循环不是。

Can't see where I'm going wrong.看不出我哪里出错了。

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);
        }
    }
}

change your entry variable inside the loop更改循环内的入口变量

int entry = 0; //Init
do
{
    Console.WriteLine(menu);

    // will throw FormatException if not int
    entry = int.Parse(Console.ReadLine());
    ...
} while (entry != 7);

Inside the loop you are not modifiying the variable "entry".在循环内部,您没有修改变量“条目”。 So unless your first entry = 7 you will be stuck in your loop, because (entry != 7) it always be true on the while (entry != 7);因此,除非您的第一个条目 = 7,否则您将被困在循环中,因为(entry != 7)while (entry != 7);上始终为真while (entry != 7);

Add the line entry = int.Parse(Console.ReadLine());添加行entry = int.Parse(Console.ReadLine()); before switch (entry) This way you will keep asking for "entry" given the possibly to entry be equal to 7. Thus, getting out of the loop.switch (entry)之前,这样你会一直要求“entry”,因为 entry 的可能等于 7。因此,退出循环。

    int entry;

    do
    {  
        Console.WriteLine(menu);

        entry = int.Parse(Console.ReadLine());
        switch (entry)
        //...
   }while (entry != 7);

As written above you have to change the entry variable inside the loop:如上所述,您必须更改循环内的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);
        }
    }
}

You have to wait for an input after the switch.您必须在切换后等待输入。 otherwise the entry can't become != 7否则条目不能变成 != 7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM