如果输入无效,如何使switch语句循环返回

问题描述 投票:0回答:2

制作比萨饼订购代码。我不确定如何在输入无效内容时如何使switch语句,Size(),Sauce(),Cheese()语句循环,因此需要重新提问。这些功能的另一个小问题是,当我运行代码时,它们实际上会打印出“ true”一词,但我不确定为什么。我对编码非常陌生,尤其是在C#中。谢谢。

namespace PizzaCreator
{
    class Program
    {
        static void Main (string[] args)
        {
            var done = false;
            do
            {
                switch (DisplayMenu())
                {
                    case Command.New: NewOrder(); break;
                    case Command.Display: DisplayOrder(); break;
                    case Command.Quit: done = true; break;
                }
            } while (!done);
        }

        enum Command
        {
            Quit = 0,
            Display = 1,
            New = 2,
        }

        static string size;
        static string meat;
        static string vegtables;
        static string sauce;
        static bool cheese;
        static bool delivery;

        private static Command DisplayMenu ()
        {
            do
            {
                Console.WriteLine("N)ew Order: ");
                Console.WriteLine("D)isplay Order: ");
                Console.WriteLine("Q)uit.");

                var input = Console.ReadLine();
                switch (input.ToLower())
                {
                    case "n": return Command.New;
                    case "d": return Command.Display;
                    case "q": return Command.Quit;

                    default: Console.WriteLine("Invalid option, please enter a valid letter. "); break;
                };
            } while (true);
        }

        private static bool ReadBoolean (string message)
        {
            Console.WriteLine(message + " (Y/N) ");
            do
            {
                string value = Console.ReadLine();
                if (!String.IsNullOrEmpty(value))
                {
                    if (String.Compare(value, "Y", true) == 0)
                        return true;
                    else if (String.Compare(value, "N", true) == 0)
                        return false;
                    char firstChar = value[0];
                };
                Console.WriteLine("Enter Y/N: ");
            } while (true);
        }

        static void NewOrder()
        { 
            Console.WriteLine(Size());
            Console.WriteLine(Sauce());
            Console.WriteLine(Cheese());
            Console.WriteLine("Meat: ");
            Console.WriteLine("Vegetables: ");
            Console.WriteLine("Delivery: (choose one)");
        }

        private static bool Size ()
        {
            Console.WriteLine("Pizza size:\n S)mall(5.00)\n M)eduium(6.25)\n L)arge(8.75)");
            Console.Write("Choose one: ");
            var choice = Console.ReadLine();
            double cost = 0;

            switch (choice)
            {
                case "S":
                 cost += 5.00;
                 break;

                 case "M":
                 cost += 6.25;
                 break;

                 case "L":
                 cost += 8.75;
                 break;

                 default:
                 Console.WriteLine("Invalid Selection please select S, M, or L");
                 break;
            }
            return (true);
        }

        private static bool Sauce ()
        {
            Console.WriteLine("Type of Sauce:\n T)raditional(0.00)\n G)arlic(1.00)\n O)regano(1.00)");
            Console.Write("Choose one: ");
            var choice = Console.ReadLine();
            double cost = 0;

            switch (choice)
            {
                case "T":
                cost += 0.00;
                break;

                case "G":
                cost += 1.00;
                break;

                case "O":
                cost += 1.00;
                break;

                default:
                Console.WriteLine("Invalid Selection please select T, G, or O");
                break;
            }
            return true;
        }

        private static bool Cheese()
        {
            double cost = 0;
            Console.WriteLine("R)eagular($0.00)\nE)xtra($1.25) ");
            do
            {
                string value = Console.ReadLine();
                if (!String.IsNullOrEmpty(value))
                {
                    if (String.Compare(value, "R", true) == 0)
                        return true;
                    else if (String.Compare(value, "E", true) == 0)
                        cost += 1.25;
                        return false;
                    char firstChar = value[0];
                };
                Console.WriteLine("Enter R/E ");
            } while (true);
        }

        static void Meat()
        {

        }

        static void Vegtables()
        {

        }

        static bool Delivery()
        {
            double cost = 0;
            Console.WriteLine("T)akeout($0.00)\nD)elivery($1.25) ");
            do
            {
                string value = Console.ReadLine();
                if (!String.IsNullOrEmpty(value))
                {
                    if (String.Compare(value, "T", true) == 0)
                        return true;
                    else if (String.Compare(value, "D", true) == 0)
                        cost += 2.50;
                        return false;
                    char firstChar = value[0];
                };
                Console.WriteLine("Enter T/D ");
            } while (true);
        }

        private static void DisplayOrder()
        {
            if (String.IsNullOrEmpty(size))
            {
                Console.WriteLine("No Pizza Selected");
                return;
            }

            Console.WriteLine(size);

            Console.WriteLine(sauce);

            Console.WriteLine(cheese ? "Cheese" : "No cheese");

            Console.WriteLine(meat);

            Console.WriteLine(vegtables);

            Console.WriteLine(delivery ? "Delivery" : "Take out");

        }
    }
}
c# switch-statement do-while
2个回答
0
投票

不确定您为什么要让他们返回布尔值,并且有很多方法可以执行此操作,但这是一个示例:

        private static bool Size ()
        {
            bool goodChoice;
            do
            {
              goodChoice = true;
              Console.WriteLine("Pizza size:\n S)mall(5.00)\n M)eduium(6.25)\n L)arge(8.75)");
              Console.Write("Choose one: ");
              var choice = Console.ReadLine();
              double cost = 0;

              switch (choice)
              {
                  case "S":
                   cost += 5.00;
                   break;

                  case "M":
                   cost += 6.25;
                   break;

                  case "L":
                   cost += 8.75;
                   break;

                  default:
                   goodChoice = false;
                   Console.WriteLine("Invalid Selection please select S, M, or L");
                   break;
              }
            } while (!goodChoice)
            return (true);
        }

[如果方法选择不当,还可能使方法返回false,然后循环返回Size,Sauce和Cheese的返回值:

bool goodValue = false;
while (!goodValue)
  goodValue = Size();

-1
投票

将switch语句包装在一个命名函数中,并在默认情况下调用该命名函数。

对不起,我不懂C#,所以没有适合您的摘录。

© www.soinside.com 2019 - 2024. All rights reserved.