【问题标题】:Find sum of elements of an array查找数组元素的总和
【发布时间】:2015-05-30 18:29:28
【问题描述】:

我需要什么计算才能找到总数?

else if (a =2) {
    TotalCredit = new int[15];
    Console.WriteLine("please enter the credits");
    int i = 0;
    for (i = 0; i < 15; i++) {
        int Credit = Convert.ToInt32(Console.ReadLine());
        Total + Credit;         
    }

    Console.WriteLine(Total);
}

【问题讨论】:

  • 你确定你的意思是a=2 而不是a==2 ??
  • 您希望 Total + Credit; 单独作为声明做什么?你的意思是TotalCredit += Credit;吗? (您还应该查找 .NET 命名约定...)

标签: c# arrays


【解决方案1】:

您需要在它使用之前声明变量Total,并且它应该在循环之前以保持其范围在循环之后可用。不仅如此,您的求和运算还应使用+= 运算符进行更正 更正如下:

     int Total=0;
     for (i = 0; i < 15; i++)
        {
            int Credit = Convert.ToInt32(Console.ReadLine());
            Total += Credit;

        }

        Console.WriteLine(Total);

【讨论】:

    【解决方案2】:

    试试这个。

    else if (a ==2)
                {
                    int[] TotalCredit = new int[15];
                    Console.WriteLine("please enter the credits");
                    int i = 0;
                    int Total = 0;
                    for (i = 0; i < 15; i++)
                    {
                        int Credit = Convert.ToInt32(Console.ReadLine());
                        Total += Credit;
    
                    }
    
                    Console.WriteLine(Total);
                }
    

    我添加了这一行int Total = 0; 来声明一个变量Total,其值为0,将总数存储在那里。 然后我将for 中的一行更改为Total += Credit;,这与Total = Total + Credit; 相同,因此每个新值都将被添加并存储到变量Total 中。

    我猜这是一个 C#,按照惯例 https://msdn.microsoft.com/en-us/library/ff926074.aspx 你最好将变量声明为小写。

    【讨论】:

    • 你的意思是else if (a == 2)
    • @kenbo 三次你也可以尝试 +=+。例如总+=+学分;它也可以工作。:)
    【解决方案3】:

    当您声明了一个整数数组时,我假设您希望保留用户输入的实际值,而不仅仅是总数。确保在 using 子句中添加 System.Linq。

      else if (a==2)
      {
          var totalCredit = new int[15];
          Console.WriteLine("please enter the credits");
          for (int i = 0; i < 15; i++)
              totalCredit[i] = Convert.ToInt32(Console.ReadLine());
    
          var total = totalCredit.Sum();
          Console.WriteLine (total);
      }
    

    【讨论】:

      【解决方案4】:

      一个好主意是优雅地验证输入,并尽量减少魔术常量15 的重复——更好的方法是为其指定一个有意义的名称(a 变量也是如此)。此外,如果您打算将每个输入存储到数组中以供以后在 else if 块之外使用,则需要在所述块之外声明它。但是,如果您不需要单个信用值,则不需要该数组。

      const int numberOfCredits = 15;
      int[] credits = new int[numberOfCredits];
      

      ...

      else if (a == 2)
      {
          int total = 0;
          int count = 0;
      
          while (count < numberOfCredits)
          {
              Console.WriteLine("Enter credit #" + (count + 1).ToString() + ":");
      
              int input;
              if (int.TryParse(Console.ReadLine(), out input))
              {
                  credits[count] = input;
                  total += input;
                  count += 1;
              }
          }
      
          Console.WriteLine(total);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-14
        • 2014-09-07
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多