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 }.
ユーザー入力を検証し、ユーザー入力を使用して計算を実行する計算機を作成しようとしています。プログラムは、ある種の名前空間定義または予想されるファイルの終わりが必要であり、中括弧が必要であることを私に伝え続けます。私が見たところ、それぞれの中括弧にはパートナーがいて、私が見る限り、すべてのインスタンスがクラス内にあるように見えます。プログラミングコースはまだ2週間ですので、初心者のように思われる場合はご容赦ください。私はこれに2日間取り組んできましたが、何が間違っているのか理解できません。私が間違っていることを知っているなら、説明してください。コンピューターが別のカーリーブレースが必要だと言っている場所の横に「/」の束を置きました。