当用户输入209,312,414

时间:2016-09-21 00:59:38

标签: c#

我需要程序在用户输入209,312,414时停止。我的while循环不起作用,因为程序继续。我做了不同的其他方式,但最后循环继续它不会停止当我输入209,312,414。我会采取任何其他建议。

using System;

        public class program
        {
            public static void Main()
            {
                const double PRICE209 = 12.99, PRICE312 = 16.77, PRICE414 = 109.07;

                double price;
                int stockNum;

                Console.Write("Please enter stock number. ");
                stockNum = Convert.ToInt32(Console.ReadLine());

                while ((stockNum == 209) || (stockNum == 312) || (stockNum == 414))
                {
                    if ((stockNum == 209) || (stockNum == 312) || (stockNum == 414))
                    {
                        if (stockNum == 209)
                        {
                            price = PRICE209;
                            Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
                        }
                        else if (stockNum == 312)
                        {
                            price = PRICE312;
                            Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
                        }
                        else if (stockNum == 414)
                        {
                            price = PRICE414;
                            Console.WriteLine("The price for item # {0} is {1}", stockNum, price.ToString("C"));
                        } // end of the else if statement
                    } // end the if statement 
                    Console.Write("Please enter stock number. ");
                    stockNum = Convert.ToInt32(Console.ReadLine());
                }  // end the while loop
                        Console.WriteLine();
                        Console.WriteLine("press <enter> to terminate program");
                        Console.ReadLine();
            }
        }

1 个答案:

答案 0 :(得分:0)

将您的while循环更改为:

while ((stockNum != 209) && (stockNum != 312) && (stockNum != 414))