namespace Calculator
{
class Program
{
static void Main(string[] args)
{
Program calc = new Program();
Program validate = new Program();
bool valid = true;
while (valid == true)
{
Console.WriteLine("Supported functions are *, /, +, -, ^, %.");
Console.WriteLine("If you would like to find the greater number separate the numbers with a '?'");
String userInput = Console.ReadLine();
valid = validate.ValEntry(userInput);
Console.WriteLine(calc.Calculate(userInput));
}/////////////////////////////////////////////////////////////////////////
private string Calculate(string input)
{
int opstringloc = findoperator(input);
int firstval = int.Parse(input.Substring(0, opstringloc));
int secondval = int.Parse(input.Substring(0, opstringloc));
char operation = Convert.ToChar(input.Substring(opstringloc));
switch (operation)
{
case '+':
return Convert.ToString(firstval+secondval);
break;
case '-':
return Convert.ToString(firstval-secondval);
break;
case '/':
return Convert.ToString(firstval/secondval);
break;
case '*':
return Convert.ToString(firstval*secondval);
break;
case '%':
return Convert.ToString(firstval%secondval);
break;
case '^':
return Convert.ToString(firstval^secondval);
break;
case '?':
if (firstval<secondval)
{
return ("[0] < [1]"); Convert.ToString(firstval); Convert.ToString(secondval);
}
else if (firstval>secondval)
{
return("[0] > [1]"); Convert.ToString(firstval); Convert.ToString(secondval);
}
else if (firstval==secondval)
{
return ("[0] = [1]"); Convert.ToString(firstval); Convert.ToString(secondval);
}
break;
default:
return ("Invalid Entry, please try again.");
break;
}
return ("Invalid Entry, please try again.");
}
private bool ValEntry(string entry)
{
for (int p = 0; p < entry.Length; p++)
if (char.IsDigit(entry[p]))
{
return true;
}
else if ((entry[p] == '+') || (entry[p] == '-') || (entry[p] == '*') || (entry[p] == '/') || (entry [p] == '%') || (entry [p] == '^') || (entry[p] == '?'))
{
return true;
}
else
{
return false;
}
return false;
}
private int findoperator (string oploc)
{
for (int loc = 0; loc < oploc.Length; loc++)
{
if (!char.IsDigit(oploc[loc])) return loc;
}
return -1;
}
}
}
} // Moving this to where it belongs shows the error in the location of the }.
我正在尝试创建一个验证用户输入的计算器,然后使用用户输入运行计算。该程序一直告诉我它需要一种命名空间定义或预期的文件结尾,并且我需要一个花括号。我看了看,每个花括号都有一个伙伴,据我所知,我的所有实例都位于类中。我只有两周的编程课程,所以如果我听起来像初学者,请原谅我。我已经为此工作了两天,但我无法弄清楚我做错了什么。如果你知道我做错了什么,请解释一下。我在计算机告诉我需要另一个花括号的位置旁边放了一堆“/”。