0

我正在尝试使用以下格式名称、D、C、D:minQ 或 R:roast 解析包含“输入 q 以退出或将整个数据作为逗号分隔的字符串”的字符串,以检查用户是否输入了“D: " 或 "R:" 并根据哪一个实例化一个特定类型的对象,Decaf decafCoffee = new Decaf for "D": and Regular regCoffee = new Regular for "R:"。解决这个问题的最简单方法是什么?

        Console.Write("Enter q to quit or the whole data as a comma delimited string using the following format Name,D,C,D:minQ or R:roast ");
        string s = Console.ReadLine();


        // Loop
        while (!s.ToLower().Equals("q"))
        {
            string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); // Trim?
            string name = values[0];
            string demand = (values[1]);
            string cost = (values[2]);
            string min = values[3];

            // Check for > 0 and convert to numbers
            float D = CheckDemand(demand);
            float C = CheckCost(cost);
            float M = CheckMin(min);

            // Create object
            Decaf decafCoffee = new Decaf
4

1 回答 1

1
Decaf decafCoffee = null;
Roast roastCoffee = null;
if (min.StartsWith("D:")) 
    decafCoffee = new Decaf();
else if (min.StartsWith("R:"))
    roastCoffee = new Roast();
else
    // Give an error or something.
于 2013-04-25T21:27:31.857 回答