2016-07-08 9 views
4

텍스트 기반 모험을 코딩 중이며 문제가 있습니다. 나는 당신이 원하는 모든 검사 작업을 처리하는 switch 문 케이스를 만들려고 노력하고, 지금까지 내 코드이 무엇입니까 :switch 문에서 변수를 사용할 수 있습니까?

case "examine" + string x: 
    //this is a method that I made that makes sure that it is an object in the area 
    bool iseobj = tut.Check(x); 
    if (iseobj) 
     x.examine(); 
    else 
     Console.WriteLine("That isn't an object to examine"); 
    break; 

은 어떻게 case 문에서 변수를 사용합니까? "examine"+ (x)로 시작하는 문자열을 대문자로 시작하고 싶습니다.

+1

"Console.WriteLine ("검사 할 개체가 아닙니다 "); switch 문에있는 – DestinatioN

+2

사례는 반드시 일정해야합니다. 변수를 포함 할 수 없습니다. –

+0

질문이 더 명확하게 편집되었으므로 올바르게 이해했는지 알려주세요. –

답변

5

시나리오가 switch 문보다 양호한 if-else 문에 적합합니다. C#에서는 switchonly evaluate values이 아니라 표현식이 될 수 있습니다. 이것은 당신이 하지 수 있다는 것을 의미합니다 :

case input.StartsWith("examine"): 

그러나, 당신이 if 문이 작품을 만들 수 있습니다! 다음을 수행하는 것을 고려하십시오 :

if (input.StartsWith("examine")) 
{ 
    //this is a method that I made that makes sure that it is an object in the area 
    bool iseobj = tut.Check(x); 
    if (iseobj) 
     x.examine(); 
    else 
     Console.WriteLine("That isn't an object to examine"); 
} 
else if (...) // other branches here 
+1

사례 StartsWith (''case 입력을 시작하십시오 .StartsWith ('? 그렇지 않으면 유쾌한 답변입니다.) – MickyD

+1

@MickyD 감사합니다. catch! –

+1

@ Fireball175 만약'x' 변수를 얻고 싶다면'if' 안에'string x = s.Split (new [] { "examine"}, StringSplitOptions.RemoveEmptyEntries) [0]'할 수 있습니다 .. –

관련 문제