2012-12-10 3 views
1

이것은 이 코드를 단축 할 방법이 있습니까?

   if (this->answer->Text == "2"){ 
       this->question->Text = "2+2?"; 
      } 
      else if (this->answer->Text == "4"){ 
       this->question->Text = "3+3?"; 
      } 
      else if (this->answer->Text == "6"){ 
       this->question->Text = "4+4?"; 
      } 
      else if (this->answer->Text == "8"){ 
       this->question->Text = "Finished!"; 
      } 
      else { 
       MessageBox::Show("Wrong!!"); 
      } 

어떤이 코드가 단축하는 것입니다 Q &에 내 코드 비주얼 C++에 게임이다? 배열 사용을 고려하십니까?

답변

2

내가 윈도우 프로그래머, 당신의 this->question->Text의 유형, 당신은 우리에게하지 않았다 무엇 모르겠어요하고, 그러나 std::string 또는 char *으로 변환 할 수있는 항목이면 작동 할 것입니다.

std::string t = this->answer->Text; 
this->question->Text = t == "2" ? "2+2?" 
        : t == "4" ? "3+3?" 
        : t == "6" ? "4+4?" 
        : t == "8" ? "Finished" 
        : ""; 
if (this->question->Text = "") MessageBox::Show("Wrong!!"); 
+0

오류 남자 오류 .. – JFetz2191

+0

@ JFetz2191 어떤 에러가 있습니까? – piokuc

+0

1 문자열 t = this-> answer-> Text; "System :: String ^"에서 "System :: String"으로 적합한 사용자 정의 변환이 없습니다. 2. "=="오류가 있습니다 .. "이 피연산자와 일치하는 연산자가 없으며 피연산자 유형은 System :: String const char [2] – JFetz2191

0

switch case 문을 사용하십시오.

+1

어떻게 코딩 할 수 있습니까? – JFetz2191

+1

그 두 번째. http://msdn.microsoft.com/en-us/library/k0t5wee3(v=vs.80).aspx – Kevin

+0

"표현식은 정수형 또는 정수로의 모호하지 않은 변환이있는 클래스 유형이어야합니다. 타입 "따라서, 적어도 여기에 시도해보십시오 –

1

if (this->answer->Text ==this->question->Text =을 반복하고 있습니다. 그것들을 한 번만 쓰고 상태와 응답을 std :: map에 유지하십시오.

업데이트 :

#include <map> 
#include <string> 
...

std::map<std::string,std::string> answers; 
answers["2"]="2+2"; // "configuration 
answers["4"]="3+3?"; 
//and so on 
std::string text=this->answer->Text; 
    // instead of if ... 
std::map<std::string,std::string>::const_iterator found=answers.find(text); 
if (found!=answers.end()) 
    this->question->Text = answers[text]; //once. Even better found->second 
else 
    MessageBox::Show("Wrong!!"); 
+0

어떻게 코딩합니까? – JFetz2191

+0

std :: map을 어떻게 사용합니까? – JFetz2191

+0

대답의 업데이트를 참조하십시오 –

관련 문제