简体   繁体   中英

Is there a way to exit this do while loop by giving the user the option to enter a number to exit?

I created an array that lets a business input zip codes that they serve, and give them the ability to search. I want to give the ability for a user to enter 0 to exit the program. How can I do this in the "while" section of the do while loop? (I am aware that entering zip codes as strings is better).

I've tried putting while(lookup != 0) and I get an error telling me that the name lookup does not exist.

int[] zipCodes = new int[5];



        for (int i = 0; i < zipCodes.Length; i = i + 1)
        {
            Console.WriteLine("Enter a 5 digit zip code that is supported in your area");
            zipCodes[i] = Convert.ToInt32(Console.ReadLine());
        }

        Array.Sort(zipCodes);
        for (int i = 0; i < zipCodes.Length; i = i + 1)
        {
            Console.WriteLine("zip codes {0}: {1}", i, zipCodes[i]);
        }


        do
        {
            Console.Write("Enter a zip code to look for: ");
            Console.WriteLine();
            Console.WriteLine("You may also enter 0 at any time to exit the program ");

            Int64 lookup = Convert.ToInt64(Console.ReadLine());
            int success = -1;


            for (int j = 0; j < zipCodes.Length; j++)
            {
                if (lookup == zipCodes[j])
                {
                    success = j;
                }
            }
            if (success == -1) // our loop changes the  -1 if found in the directory
            {
                Console.WriteLine("No, that number is not in the directory.");
            }

            else
            {
                Console.WriteLine("Yes, that number is at location {0}.", success);
            }
        } while (lookup != 0);

        Console.ReadLine();

Input zip codes that they serve, and give them the ability to search. Display the zip codes entered into the array, then give the option to search or exit the program.

Like I said in the comment above: you need to define the lookup variable outside of your do while loop, it only currently exists within, hence when the condition is ran it causes an error :)

Int64 lookup = 1; //or something other than 0
do
{
   ...
    your code
   ...
} while (lookup != 0);

Generally whenever you declare a variable in a block of code that is bounded by {} (such as if or while ), the variable will only exist inside that block. To answer your question, your lookup variable exists only inside the while loop and therefore cannot be used in the condition. To prevent this, define it outside your loop.

Int64 lookup = 1;
do
{
    Console.Write("Enter a zip code to look for: ");
    Console.WriteLine();
    Console.WriteLine("You may also enter 0 at any time to exit the program ");

    lookup = Convert.ToInt64(Console.ReadLine());
    int success = -1;


    for (int j = 0; j < zipCodes.Length; j++)
    {
        if (lookup == zipCodes[j])
        {
            success = j;
        }
    }
    if (success == -1) // our loop changes the  -1 if found in the directory
    {
        Console.WriteLine("No, that number is not in the directory.");
    }

    else
    {
        Console.WriteLine("Yes, that number is at location {0}.", success);
    }
} 
while (lookup != 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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