类型或命名空间定义,或期望的文件结尾; “}”预期的错误

时间:2013-01-21 19:37:42

标签: c# visual-studio-2012 calculator

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 }.

我正在尝试创建一个验证用户输入的计算器,然后使用用户输入运行计算。程序一直告诉我它需要一种命名空间定义或期望的文件结尾,我需要一个大括号。我看了,每个大括号都有一个伙伴,据我所知,它看起来像我的所有实例都在这个类中。我的编程课程只有两周,所以如果我听起来像初学者,请原谅我。我已经在这两天工作了,我无法弄清楚我做错了什么。如果你知道我做错了什么,请解释一下。我把一堆“/”放在计算机告诉我需要另一个大括号的地方旁边。

3 个答案:

答案 0 :(得分:3)

您的主要方法在移至private string Calculate()之前未被关闭。在while结束后应该有另一个}。然后,您只需要删除文档末尾的}之一。

答案 1 :(得分: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;
        }


    }
}

答案 2 :(得分:0)

您没有正确计算。一个

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

并在结尾删除一个。

然后我只需将String更改为string。更可能......