我需要将这些If语句转换为Switch-case语句。我这样做时会出现语法错误。我试过用smth。像:
switch(field.text){
case field.text.contains("+"):
field.text=a+b;
break;
}
但" field.text.contains(" +")"有语法错误。
我需要帮助。 这是我的代码:
这是计算器应用程序,执行简单的2运算符数学运算。
这是将字符串拆分为2以便找到2个输入数字的方法:
public void GetNumbers(ref string first, ref string second, int place, out int firstNumber, out int secondNumber)
{
if (place > 0)
{
first = ResultFiled.Text.Substring(0, place);
second = ResultFiled.Text.Substring(place + 1);
}
Int32.TryParse(first, out firstNumber);
Int32.TryParse(second, out secondNumber);
}
private void Calculate_Click(object sender, EventArgs e)
{
string first = null;
string second = null;
int result = 0;
int place = 0;
int firstNumber = 0;
int secondNumber = 0;
在计算器表单中标识输入数字中的运算符,然后找到它的位置。将字符串拆分为2,以查找第一个和最后一个数字。然后做数学运算。
添加2个数字
if (ResultFiled.Text.Contains("+"))
{
place = ResultFiled.Text.IndexOf("+");
GetNumbers(ref first, ref second, place, out firstNumber, out secondNumber);
result = firstNumber + secondNumber;
ResultFiled.Text = result.ToString();
}
else
{
减去2个数字
if (ResultFiled.Text.Contains("-"))
{
place = ResultFiled.Text.IndexOf("-");
GetNumbers(ref first, ref second, place, out firstNumber, out secondNumber);
result = firstNumber - secondNumber;
ResultFiled.Text = Convert.ToString(result);
}
else
{
乘以2个数字
if (ResultFiled.Text.Contains("*"))
{
place = ResultFiled.Text.IndexOf("*");
GetNumbers(ref first, ref second, place, out firstNumber, out secondNumber);
result = firstNumber * secondNumber;
ResultFiled.Text = Convert.ToString(result);
}
else
{
除以2个数字
if (ResultFiled.Text.Contains("/"))
{
place = ResultFiled.Text.IndexOf("/");
GetNumbers(ref first, ref second, place, out firstNumber, out secondNumber);
try
{
result = firstNumber / secondNumber;
}
catch (DivideByZeroException exc)
{
MessageBox.Show(exc.Message);
}
ResultFiled.Text = Convert.ToString(result);
}
}
}
}
}
答案 0 :(得分:3)
首先创建枚举MathOp
:
enum MathOp {Null, Add, Sub, Mult, Div }
然后:
var op =
txt.Contains("+")? MathOp.Add:
txt.Contains("-")? MathOp.Sub:
txt.Contains("*")? MathOp.Mult:
txt.Contains("/")? MathOp.Div:
MathOp.Null;
switch (op)
{
case (MathOp.Add):
...
case (MathOp.Sub):
...
}
答案 1 :(得分:0)
如果您也拆分了运算符(+, - 等),您可以执行类似以下操作:
public enum Operator {
Plus,
Minus
}
Operator operator = Operator.Plus; // As an example.
switch (operator) {
case Operator.Plus:
Console.WriteLine("Here's the plus operator");
break;
case Operator.Minus:
Console.WriteLine("Here's the minus operator");
break;
default:
Console.WriteLine("This isn't an operator");
break;
然后,在发生Console.WriteLine
的操作发生时替换您要运行的代码。这是一个很好的格式,分成函数:
public int PerformOperation(int firstNumber, int secondNumber, string operator) {
switch (operator) {
case Operator.Plus:
Console.WriteLine("Here's the plus operator");
return (firstNumber + secondNumber);
break;
case Operator.Minus:
return (firstNumber - secondNumber);
break;
default:
Console.WriteLine("This isn't an operator");
break;
然后,您可以按如下方式编写逻辑:
void SomeMainFunction() {
string textBoxInput = txbInput.Text; // Example of fetching calc input
int firstNumber = GetFirstNumber(textBoxInput); // Write a function that returns your firstNumber from the input string.
int firstNumber = GetSecondNumber(textBoxInput); // Write a function that returns your secondNumber from the input string.
string operator = GetOperator(textBoxInput); // Write a function that returns your operator from the input string.
txbInput.Text = PerformOperation(firstNumber, secondNumber, operator);
}