2013-04-25 2 views
0

"Q를 입력하여 종료하거나 전체 데이터를 이름, D, C, D 형식을 사용하여 쉼표로 구분 된 문자열로 포함합니다. minQ 또는 R : 로스트 "사용자가"D : "또는"R : "을 입력했는지 확인하고 특정 유형의 객체를 인스턴스화하는 경우 Decaf decafCoffee ="D "의 새 Decaf : Regular regCoffee = new"R : ". 이 문제를 해결하는 가장 쉬운 방법은 무엇입니까? 문자열의 마지막 값 내에서 "D :"또는 "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 
+0

와'if' 문 – OscarRyz

+0

는 어떻게 값을 찾을 수 있습니까? –

+0

형식이 변경됩니까? 그렇지 않으면'IndexOf' 또는'SubString'을 시도하십시오. –

답변

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.