我正在尝试一些看起来超级简单的事情,但现在我想把我的显示器扔到外面的雪地里。我不明白为什么我的 switch 语句在调用时没有执行。
这里是:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) // Arithmetic Button Click
{
String^ riS = dc1->Text;
String^ ciS = dc2->Text;
colint = int::Parse(dc2->Text);
std::ostringstream ss;
String^ answer;
op1 = Double::Parse(foValue->Text);
op2 = Double::Parse(soValue->Text);
// Enter switch
switch(op_sym)
{
case '+':
sum = op1 + op2;
DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
ss << sum;
answer = Convert::ToString(answer);
MessageBox::Show(answer);
DGV->CurrentCell->Value = answer;
sumLabel->Text = "TEST";
break;
case '-':
sum = op1 - op2;
break;
case '*':
sum = op1 * op2;
break;
case '/':
if (op2 == 0)
{
MessageBox::Show("Sorry, you cannot divide by zero \n Please, reselect yoru second cell operand");
secondOpText->Text = "";
}
else
{
sum = op1/op2;
}
break;
default:
MessageBox::Show("I'm sorry. Please select one of these four arithmetic symbols from the drop down list: \n +, -, *, /");
break;
}
}
我从上面得到 op_sym :
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
Object^ selectedItem = comboBox1->SelectedItem;
String^ cb = selectedItem->ToString();
if( cb = "+")
{
op_sym = '+';
}
if(cb = "-")
op_sym = '-';
if(cb = "/")
op_sym = '/';
if(cb = "*")
op_sym = '*';
}
op_sym 已在顶部声明为 char。如果有人能告诉我我最有可能的初学者错误,我将不胜感激。谢谢。
编辑
...
case '+':
{
sum = op1 + op2;
DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
ss << sum;
answer = Convert::ToString(sum);
MessageBox::Show( answer);
DGV->CurrentCell->Value = answer;
sumLabel->Text = answer;
break;
}
...
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
{
Object^ selectedItem = comboBox1->SelectedItem;
String^ cb = selectedItem->ToString();
if( cb == "+")
{
op_sym = '+';
}
if(cb == "-")
op_sym = '-';
if(cb == "/")
op_sym = '/';
if(cb == "*")
op_sym = '*';
}