2016-12-16 80 views
-3

我只是一个初学者每一行,我有一个文本文件是这样的:如何得到数字的总和在

1 2 3 4 5(他们都在不同的线路)

如何总结这些数字得到15在C#?

+4

这是你的功课吗? – Sam07

+4

找到他们,解析他们,添加他们。 – Plutonix

+2

@waynechen本论坛旨在提出有关代码的非常具体的问题。一旦你开始了,我们将帮助你解决具体问题。此外,这看起来更像是一项家庭作业。 “教一个人钓鱼,他会吃1000年,给一个人一些鱼,他会饿死”。 – logixologist

回答

0

将你的号码放在bin/Debug的file.txt中。 把它放进你的Program.cs中

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var nums = File.ReadAllLines("file.txt"); 
      var total = 0; 
      foreach (var str in nums) 
      { 
       total += int.Parse(str); 
      } 
      Console.WriteLine(total); 
     } 
    } 
} 
+0

在他的情况下不起作用,因为他的文本文件每行有1个数字,而不是像样本中的所有数字。 –

+0

感谢您的评论。相应更新。 –