简体   繁体   English

计算器计算多个数字

[英]calculator computing multiple numbers

static void Main(string[] args)
{
    string again;
    do
    {
        Console.Write("Enter size to compute: ");
        int size = Convert.ToInt32(Console.ReadLine());
        int[] numbers = new int[size];
        float[] numberS = new float[size];
        Console.Write("Pick one of the operation \"(+) (-) (*) (/)\": ");
        string operation = Console.ReadLine();

        if (operation == "+")
        {
            for (int i = 0; i < size; i++)
            {
                Console.Write("Enter numbers: ");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("The sum is:" + add(numbers));
        }
        else if (operation == "-")
        {
            for (int i = 0; i < size; i++)
            {
                Console.Write("Enter numbers: ");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("The subtraction is:" + subtract(numbers));
        }
        else if (operation == "*")
        {
            for (int i = 0; i < size; i++)
            {
                Console.Write("Enter numbers: ");
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("The mulplication is:" + multiply(numbers));
        }
        else if (operation == "/")
        {
            for (int i = 0; i < size; i++)
            {
                Console.Write("Enter numbers: ");
                numberS[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("The division is:" + division(numberS));
        }
        else Console.WriteLine("Invalid Input");

        Console.Write("Do you want to compute again Y/N: ");
        again = Console.ReadLine().ToUpper();
        Console.Clear();
    } while (again == "Y");
}
static int add(int[] numbers)
{
    int total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        total += numbers[i];
    }
    return total;
}
static int subtract(int[] numbers)
{
    int total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        total += numbers[i] - numbers[i];
    }
    return total;
}
static int multiply(int[] numbers)
{
    int total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        total += numbers[i] * numbers[i];
    }
    return total;
}
static float division(float[] numbers)
{
    float total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        total += numbers[i] / numbers[i];
    }
    return total;
}

I was expecting the same results in my phones calculator but no我期待在我的手机计算器中得到相同的结果,但没有

Your code itself is fine.您的代码本身很好。 However your subtract , multiply and division methods are wrong.但是,您的subtractmultiplydivision方法是错误的。 They are calculating something totally different than intended.他们正在计算与预期完全不同的东西。

The multiply-method sums the squares of the entered numbers.乘法对输入数字的平方求和。

The subtract-method always subtracts the current number of itself which equals 0 and always results with a total of 0. subtract-method 总是减去等于 0 的自身的当前数,结果总为 0。

The division method always divides the current number by itself which is 1 and results with a total which equals the number of numbers entered.除法方法总是将当前数字除以本身,即 1,结果总数等于输入的数字数。

Try these methods instead:试试这些方法:

static int subtract(int[] numbers)
{
    if (numbers.Length == 0)
        return 0;

    int total = numbers[0];
    for (int i = 1; i < numbers.Length; i++)
    {
        total -= numbers[i];
    }
    return total;
}

static int multiply(int[] numbers)
{
    if (numbers.Length == 0)
        return 0;

    int total = numbers[0];
    for (int i = 1; i < numbers.Length; i++)
    {
        total *= numbers[i];
    }

    return total;
}

static float division(float[] numbers)
{
    if (numbers.Length == 0)
        return 0;

    float total = numbers[0];
    for (int i = 1; i < numbers.Length; i++)
    {
        total /= numbers[i];
    }

    return total;
}

In all of these methods the program first checks if there are any numbers passed.在所有这些方法中,程序首先检查是否有任何数字通过。 Otherwise it just returns 0.否则它只返回 0。

If there are any numbers you set the first entered number to the total variable because it will be used anyway.如果有任何数字,则将第一个输入的数字设置为总变量,因为无论如何都会使用它。 The temporary result is always stored in the total variable.临时结果总是存储在total变量中。

Because the first element is already used the for-loop starts from index 1 instead of 0.因为第一个元素已经被使用,for 循环从索引 1 而不是 0 开始。

Then the operations are applied to the variable with all remaining numbers.然后将操作应用于具有所有剩余数字的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM