如何将持续计算添加到我的C#计算器?
作为一个例子,我有2个数字,它们总和为5,我希望能够从之前的结果中添加5个。我如何实现这个目标?
public class regneoperato : tilfældig
{
public static void hej()
{
int number1 = Convert.ToInt32(Console.ReadLine());
string regneoperator = Console.ReadLine();
int number2 = Convert.ToInt32(Console.ReadLine());
int result = 0;
switch (regneoperator)
{
case "+":
result = number1 + number2;
break;
case "-":
result = number1 - number2;
break;
case "/":
if (number2 == 0)
{
Console.WriteLine("Jeg kan ikke dividere med 0");
}
result = number1 / number2;
break;
case "*":
result = number1 * number2;
break;
}
Console.WriteLine(result);
}
}
答案 0 :(得分:3)
只需将一个参数设置为结果
即可var number1 = Convert.ToInt32(Console.ReadLine());
do
{
//everything else
number1 = result;
}while(/*insert condition here*/);