c#(初学者)如何制作这个“if”代码的简短版本?

时间:2017-05-04 22:08:41

标签: c#

我刚刚学会了如何使用if并尝试做一个简单的计算器。但代码太长了,我想为它添加更多选项,我可以使用任何其他方法缩短它吗?

namespace ConsoleApp4
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("How many numbers do you wish to calculate? ");
        string HowMany = Console.ReadLine();
        if (HowMany == "2") {
            Console.Write("Enter #1: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter #2: ");
            int y = int.Parse(Console.ReadLine());
            int sum = x + y; int sub = x - y; int mult = x * y; int div = x / y;
            Console.WriteLine("The Result : Sum = " + sum + " Sub = " + sub + " Mult = " + mult + " Div = " + div); }
        else if (HowMany == "3") {
            Console.Write("Enter #1: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter #2: ");
            int y = int.Parse(Console.ReadLine());
            Console.Write("Enter #3: ");
            int z = int.Parse(Console.ReadLine());
            int sum = x + y + z; int sub = x - y - z; int mult = x * y * z; int div = x / y / z;
            Console.WriteLine("The Result : Sum = " + sum + " Sub = " + sub + " Mult = " + mult + " Div = " + div); }
        else if (HowMany == "4")
        {
            Console.Write("Enter #1: ");
            int x = int.Parse(Console.ReadLine());
            Console.Write("Enter #2: ");
            int y = int.Parse(Console.ReadLine());
            Console.Write("Enter #3: ");
            int z = int.Parse(Console.ReadLine());
            Console.Write("Enter #4: ");
            int w = int.Parse(Console.ReadLine());
            int sum = x + y + z + w; int sub = x - y - z - w; int mult = x * y * z * w; int div = x / y / z / w;
            Console.WriteLine("The Result : Sum = " + sum + " Sub = " + sub + " Mult = " + mult + " Div = " + div);
        }

        Console.ReadKey();
    }
 }
}

3 个答案:

答案 0 :(得分:3)

这只是总和。我将处理其他计算给你。

int sum = 0;
for(int i = 1;i<=HowMany;i++)
{
    Console.Write("Enter #{0}: ", i);
    int input = int.Parse(Console.ReadLine());
    sum += input;
}
Console.WriteLine("The Result : Sum = {0}", sum);

答案 1 :(得分:0)

using System.IO;
using System;

class Program {
 static void Main() {
  Console.WriteLine("How many numbers do you wish to calculate? ");
  string HowMany = Console.ReadLine();

  int[] nums = new int[int.Parse(HowMany)];
  int sum = 0, sub = 0, mult = 1, div = 1;
  for (int i = 0; i < int.Parse(HowMany); i++) {
   Console.Write("Enter #" + (i + 1) + ": ");
   nums[i] = int.Parse(Console.ReadLine());
   sum += nums[i];
   sub -= nums[i];
   mult *= nums[i];
   div /= nums[i];
  }

  Console.WriteLine("The Result : Sum = " + sum + " Sub = " + sub + " Mult = " + mult + " Div = " + div);

  Console.ReadKey();
 }
}

答案 2 :(得分:0)

您可以使用循环,并保持值的滚动总和。另外,我将div更改为double,因为整数除法真的很无聊 - 很高兴看到小数值:

我也这样做了,所以用户只能为他们想要计算的数字量输入一个大于零的整数。

最后,他们只能为数字输入一个非零整数,因为零输入意味着我们必须在除法期间对其进行一些检查,乘法值将始终为零,并且它对加法没有任何作用和减法总数。

Console.Write("How many numbers do you wish to calculate? ");
int count;
while (!int.TryParse(Console.ReadLine(), out count) || count < 1)
{
    Console.Write("Error: enter a positive, whole number: ");
}

int input, sum = 0, sub = 0, mult = 0;
double div = 0;

for (int i = 1; i <= count; i++)
{
    Console.Write($"Enter #{i}: ");

    // Don't allow user to enter 0. It does nothing to the addition or subtraction
    // values, will make the multiplication value zero, and cannot be use for division
    while (!int.TryParse(Console.ReadLine(), out input) || input == 0)
    {
        Console.Write("Error: enter a non-zero whole number: ");
    }

    if (i == 1)
    {
        // On the first input, we just store the number
        sum = sub = mult = input;
        div = input;
    }
    else
    {
        sum += input;
        sub -= input;
        mult *= input;
        div /= input;
    }
}

Console.WriteLine("The Result : Sum = " + sum + " Sub = " + 
    sub + " Mult = " + mult + " Div = " + div);

Console.ReadKey();