我试图做一个函数的计算器,但是当我想返回我的值的'计算',它会给我的函数中变量的值是0而不是计算。 当我编写正确的操作符时,也会显示默认文本。c#控制台应用程序简单的计算器不工作
static void Main(string[] args)
{
string input1,input2,opera,product = "";
Int32 v,n;
Console.WriteLine("please insert first number: ");
input1 = Console.ReadLine();
Console.WriteLine("please insert second number: ");
input2 = Console.ReadLine();
if (numeric(input1, out v) && numeric(input2, out n))
{
Console.WriteLine("give operator: *, /, -, +");
opera = Console.ReadLine();
Console.WriteLine(operat(product));
}
private static string operat(string oper){
double input1 = 0, input2 = 0;
double calculation = 0;
switch (oper)
{
case "*":
calculation += input1 * input2;
break;
case "/":
calculation += input1/input2;
break;
case "+":
calculation += input1 + input2;
break; ;
case "-":
calculation += input1 - input2;
return calculation.ToString();
default:
Console.WriteLine("you gave the write operator...");
break;
}
return calculation.ToString();
忽略的数值函数...
当调用'operat'您正在使用'product'变量,它似乎没有设置。 –
哈哈!试想一下......提示:0次0等于什么? –
看起来像是一个用调试器练习的好机会。逐步浏览代码,在每一步检查每个变量的值。当你指出某个变量的值不是预期的值时,找出原因并解决该问题。冲洗,重复。 –