我怎么解决这个问题?我在节目中解释

时间:2014-10-06 23:39:39

标签: if-statement compiler-errors switch-statement

//我的程序中犯了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.


        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果你声明你的字符串错误,最好使用:string error = ""; 在你的情况下:error = "My Error!";

并写下来:

Console.WriteLine(error);
Console.ReadLine(); //wait for input from keyboard.
相关问题