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

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

4

3 回答 3

3

您的 Main 方法在移至private string Calculate(). 结束后应该有另一个} while。然后,您只需删除}文档末尾的其中一个。

于 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

你没有数错。一后

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

并在最后删除一个。

然后我只需将字符串更改为字符串。更有可能...

于 2013-01-21T19:41:57.233 回答