0
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日間取り組んできましたが、何が間違っているのか理解できません。私が間違っていることを知っているなら、説明してください。コンピューターが別のカーリーブレースが必要だと言っている場所の横に「/」の束を置きました。

4

3 に答える 3

3

Main メソッドは、に移動する前に閉じられていませんprivate string Calculate()。の終了後に別の } があるはずwhileです。}次に、ドキュメントの末尾にある の1 つを削除するだけです。

于 2013-01-21T19:41:22.910 に答える
1

while ループの終了直後に閉じ括弧 } がありませんでした。

これを試して:

namespace Calculator
{
    internal class Program
    {
        private 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;
        }


    }
}
于 2013-01-21T19:44:38.607 に答える
0

あなたは正しく数えていません。1つ後

Console.WriteLine(calc.Calculate(userInput));

最後に1つを削除します。

次に、文字列を文字列に変更します。そして、おそらく...

于 2013-01-21T19:41:57.233 に答える