C# 在c语言中使用windows窗体添加矩阵组件#
我是一名新的C#程序员,我想用Windows窗体编写一个程序,可以从文本框中获取邻接矩阵,然后返回矩阵中1的和。我正在编写输入矩阵的代码,但我不知道如何对它们的分量求和C# 在c语言中使用windows窗体添加矩阵组件#,c#,matrix,C#,Matrix,我是一名新的C#程序员,我想用Windows窗体编写一个程序,可以从文本框中获取邻接矩阵,然后返回矩阵中1的和。我正在编写输入矩阵的代码,但我不知道如何对它们的分量求和 string s = txtFirstgraph.Text; string[] words = s.Split(','); List<string[]> matrix = new List<string[]>(); for (int i = 0; i < words.Count(); i++)
string s = txtFirstgraph.Text;
string[] words = s.Split(',');
List<string[]> matrix = new List<string[]>();
for (int i = 0; i < words.Count(); i++)
{
if (txtFirstedge.Text[i] == '0' || txtFirstedge.Text[i] == '1' )
{
string[] temp = words[i].Split(' ');
matrix.Add(temp);
}
else
{
MessageBox.Show("you only can enter 0 or 1 for adjancy matrix please try again");
this.Close();
}
}
string s=txtFirstgraph.Text;
string[]words=s.Split(',');
列表矩阵=新列表();
for(int i=0;i
没有上下文,代码毫无意义,但对列表中的元素求和应该非常简单:
var sum=matrix.Sum(w=>w.Sum(q=>Convert.ToInt32(q));
我想提出的一点是,你的邻接矩阵应该是整数,而不是字符串。事实上,如果需要,它可以是布尔位字段。但同样,您的代码毫无意义,因此您可能有理由这样做。请向我们展示一个示例。例如,什么是
txtFirstEdge
?它没有定义。txtFirstedge是一个文本框,我可以在其中输入矩阵组件。这只是一个条件,matirx只能有0或1,因为它的元素会导致堆栈溢出!我改进了你问题的拼写和语法,以帮助人们更好地帮助你。同样,我缩进了你的代码,这样更容易阅读。祝你好运