Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试着用C#制作一个计算器。这里出了什么问题?_C#_Error Handling_Calculator - Fatal编程技术网

试着用C#制作一个计算器。这里出了什么问题?

试着用C#制作一个计算器。这里出了什么问题?,c#,error-handling,calculator,C#,Error Handling,Calculator,我对C#相当陌生,所以我为这个补救问题道歉。 问题是,当我运行并输入1,然后输入2和4时,返回82,然后菜单被打印两次。这显然是不正确的。有人能告诉我为什么会这样吗?我想这与为什么我的转换有关,但我想确切地知道为什么语言的行为是这样的,因为这似乎应该是可行的。谢谢你的帮助 编辑:我不知道为什么我被否决了,请让我知道我做错了什么 以下是一个示例输出: class Program { static void Main(string[] args) { bool run

我对C#相当陌生,所以我为这个补救问题道歉。 问题是,当我运行并输入1,然后输入2和4时,返回82,然后菜单被打印两次。这显然是不正确的。有人能告诉我为什么会这样吗?我想这与为什么我的转换有关,但我想确切地知道为什么语言的行为是这样的,因为这似乎应该是可行的。谢谢你的帮助

编辑:我不知道为什么我被否决了,请让我知道我做错了什么

以下是一个示例输出:

class Program
{
    static void Main(string[] args)
    {
        bool run = true;
        do
        {
            Console.WriteLine("Make a choice or type 0 to exit: ");
            Console.WriteLine("1. Add 2 numbers\n2. Subtract 2 numbers\n3.Multiply 2 numbers\n4. Divide 2 numbers");
            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 0)
            {
                run = false;
            }
            if(choice == 1)
            {
                int x, y;
                Console.Write("Enter 2 numbers to Operate on: ");
                x = Convert.ToInt32(Console.Read());
                y = Convert.ToInt32(Console.Read());
                Console.WriteLine("The Result is: {0}", Convert.ToInt32(add(x,y)));
            }

        }while(run);
        Console.ReadKey();
    }

    public static int add(int x, int y)
    {
        return x+y;
    }

    public static int sub(int x, int y)
    {
        return x - y;
    }

    public static int mult(int x, int y)
    {
        return x * y;
    }

    public static double div(int x, int y)
    {
        return (float)x / y;
    }
问题在于:

Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
1
Enter 2 numbers to Operate on: 2 4
The Result is: 82
Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
Make a choice or type 0 to exit:
1. Add 2 numbers
2. Subtract 2 numbers
3. Multiply 2 numbers
4. Divide 2 numbers
您不应在此处使用
阅读
<代码>读取
读取单个字符并将其转换为相应的ASCII值。基本上,您将同时添加到ASCII值

解决方案:

您只需将其更改为
ReadLine

x = Convert.ToInt32(Console.Read());
y = Convert.ToInt32(Console.Read());
但是,如果希望两个数字之间用空格分隔,如
2 4
,可以执行以下操作:

x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());

请阅读文档以了解更多信息。它做了你期望它做的事吗?(另外,请注意,它已经返回了一个
int
,正如
add
,因此您的转换目前完全没有意义)Damien,它说它应该读取下一个字符。如果我输入2个整数,它不应该像我写的那样背对背地读取它们吗?你似乎输入了3个字符,
2
,一个空格,和
4
(后跟enter)-所以你们两个读取
2
字符和空格字符。如果您输入了
2
2
,一个空格,
4
和一个
1
,后跟enter),即字符串
22 41
,则两次读取将读取两个
2
字符。那么,这就是您所期望的吗?C#是否将空格识别为“2”,将enter识别为“1”?Console.Read()仅从标准输入中读取下一个字符。我建议您使用Console.ReadLine()并要求用户分别输入每个数字。Readline()从标准输入流中读取下一行字符,这样就不会出现这种问题。太棒了,它解决了这个问题,而且很有意义。谢谢你不仅回答了我的问题,还解释了我做错了什么。非常感谢。使用Int32.TryParse可以避免在用户输入非数字输入时出现异常
string[] numbers = Console.ReadLine().Split(' ');
x = Convert.ToInt32(numbers[0]);
y = Convert.ToInt32(numbers[1]);