我正在尝试制作一个简单的计算器,但出现此错误“使用未分配的变量'运算'
它仅在语句开头的一行中给出错误
此处:if( operation ==“ x”)
整件事:
static void Main()
{
int firstNumber;
int secondNumber;
string operation;
int answer;
Console.WriteLine("Hello welcome to Eral's Calculator");
Console.ReadLine();
Console.Write("Enter the first number for the equation");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number for the equation");
secondNumber = Convert.ToInt32(Console.ReadLine());
if (operation == "x")
{
answer = firstNumber * secondNumber;
Console.WriteLine(firstNumber + "x" + secondNumber + " = " + answer);
Console.ReadLine();
}
else if (operation == "+")
{
answer = firstNumber + secondNumber;
Console.WriteLine(firstNumber + "+" + secondNumber + " = " + answer);
Console.ReadLine();
}
else if (operation == "-")
{
answer = firstNumber - secondNumber;
Console.WriteLine(firstNumber + "-" + secondNumber + " = " + answer);
Console.ReadLine();
}
else if (operation == "/")
{
answer = firstNumber / secondNumber;
Console.WriteLine(firstNumber + "/" + secondNumber + "=" + answer);
Console.ReadLine();
}
else
{
Console.WriteLine("That is not how you calculate, Try Again?");
Console.ReadLine();
}
}
答案 0 :(得分:3)
您需要捕获operation
的值。
例如:
Console.WriteLine("Enter the operation type");
operation = Console.ReadLine();
答案 1 :(得分:-1)
在我看来,操作从未设置为任何东西?