我该如何解决?我在程序中解释

麦可

//我在程序中犯了3个错误。我想让我的所有错误都显示在最后。因此,如果有任何错误,程序将显示错误并关闭控制台,否则,将进行数学计算

 class Program
    {
        static void Main(string[] args)
        {
            string name;
            string color;
            string shipping;
            string error;

            int shirtCost;
            int shippingCost;
            int quantity;

            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("  Welcome to Conestoga Online Shirts store");
            Console.WriteLine("---------------------------------------------");

            Console.WriteLine(" Please add your name:" + Environment.NewLine);
            name = Console.ReadLine();

            Console.WriteLine(Environment.NewLine + "What colour would you like to buy " + name + "?" + Environment.NewLine);
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine("Green = $20, Blue = $20, Yellow = $10, Brown = $5, Other = $15" + Environment.NewLine);
            Console.WriteLine("--------------------------------------------------------------------------------" + Environment.NewLine);
            color = Console.ReadLine();

            switch (color)
            {
                case "green":
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(Environment.NewLine + "Your color is Green" + Environment.NewLine);
                    shirtCost = 20;
                    break;
                case "blue":
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(Environment.NewLine + "Your color is Blue" + Environment.NewLine);
                    shirtCost = 20;
                    break;
                case "yellow":
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(Environment.NewLine + "Your color is Yellow" + Environment.NewLine);
                    shirtCost = 10;
                    break;
                case "brown":
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(Environment.NewLine + "Your color is Brown" + Environment.NewLine);
                    shirtCost = 5;
                    break;
                case "other":
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(Environment.NewLine + "Your color is Other" + Environment.NewLine);
                    shirtCost = 15;
                    break;
                 default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    error = "Error: Choose the right color.";  //i want this error show up at the end
                    break;
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(Environment.NewLine + "How many would you like to order?" + Environment.NewLine);
            Console.WriteLine("!!!!!!!Notic: Customer cane choose Between 1 to 200!!!!!!!" + Environment.NewLine);
            quantity = int.Parse(Console.ReadLine());

            if ((quantity >= 1) && (quantity <= 200))
            {
                Console.WriteLine(Environment.NewLine + "Your quantity is " + quantity + Environment.NewLine);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                error = "Error: Choose the right Number."; //i want this error show up at the end
            }

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("What kind of shipping method you want to choose?" + Environment.NewLine);
            Console.WriteLine("-----------------------------------------------------------------------");
            Console.WriteLine("Expedited = $10, Standard = $5, None = $0");
            Console.WriteLine("-----------------------------------------------------------------------" + Environment.NewLine);
            shipping = Console.ReadLine();

            switch (shipping)
            {
                case "expedited":
                    Console.WriteLine("Youe shipping is Expedited");
                    shippingCost = 10;
                    break;
                case "standard":
                    Console.WriteLine("Youe shipping is Standard");
                    shippingCost = 5;
                    break;
                case "none":
                    Console.WriteLine("Youe shipping is None");
                    shippingCost = 0;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    error = "Error: Choose the right shipping method.";//i want this error show up at the end
                    break;
            }

            Console.ForegroundColor = ConsoleColor.Gray;

// i want my error show up here. so if there are any error. show up the error and than close the console, but if there are not any error, will do the math.


        }
    }
}
马雷克·沃尼亚克(MarekWoźniak)

如果你declarating你的字符串错误,更好的使用:string error = ""; 那么在你的情况下:error = "My Error!";

并写成:

Console.WriteLine(error);
Console.ReadLine(); //wait for input from keyboard.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章