多维数组中的列 c#
Columns in Multidimensional Arrays c#
我在 txt 中有这样的数据:
跳蚤,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,6
青蛙,0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,5
青蛙,0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,5
我需要计算所选列中零的数量,例如在第一列中有 3 个零。
到目前为止,这是我的代码:
//data patch
string[] tekst = File.ReadAllLines(@"C:\zoo.txt");
//full of array
string[] tablica = tekst;
for(int s=0;s<tablica.Length;s++)
{
Console.WriteLine(tablica[s]);
}
//----------------Show all of array---------------------------//
//----------------Giv a number of column-----------------////
Console.WriteLine("Podaj kolumne");
int a = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("Podaj wiersz");
//int b = Convert.ToInt32(Console.ReadLine());
int n = tablica.Length;
int m = tablica[a].Split(',').Length;
string[,] liczby = new string[n, m];
for (int j = 0; j < n; j++)
{
int suma = 0;
for (int i = 0; i < m; i++)
{
//somethink should be here
}
}
有解决这个问题的想法吗?
尝试:
//data patch
string[] tekst = File.ReadAllLines(@"C:\zoo.txt");
//full of array - you allready have a string[], no need for a new one
//string[] tablica = tekst;
for(int s=0;s<tekst.Length;s++)
{
Console.WriteLine(tekst[s]);
}
//----------------Show all of array---------------------------//
//----------------Giv a number of column-----------------////
// try to use names with some meaning for variables, for example instead of "a" use "column" for the column
Console.WriteLine("Podaj kolumne");
int column = Convert.ToInt32(Console.ReadLine());
// your result for zeros in a given column
int suma = 0;
// for each line in your string[]
foreach ( string line in tekst )
{
// get the line separated by comas
string[] lineColumns = line.Split(',');
// check if that column is a zero, remember index is base 0
if ( lineColumns[column-1] == "0" )
suma++;
}
Console.WriteLine(suma);
编辑:只需确保验证他们要求的列确实存在于您的数组中,并且如果您不认为第一列是具有名称的列,请调整此部分
lineColumns[column] // instead of lineColumns[column-1]
我在 txt 中有这样的数据:
跳蚤,0,0,1,0,0,0,0,0,0,1,0,0,6,0,0,0,6
青蛙,0,0,1,0,0,1,1,1,1,1,0,0,4,0,0,0,5
青蛙,0,0,1,0,0,1,1,1,1,1,1,0,4,0,0,0,5
我需要计算所选列中零的数量,例如在第一列中有 3 个零。
到目前为止,这是我的代码:
//data patch
string[] tekst = File.ReadAllLines(@"C:\zoo.txt");
//full of array
string[] tablica = tekst;
for(int s=0;s<tablica.Length;s++)
{
Console.WriteLine(tablica[s]);
}
//----------------Show all of array---------------------------//
//----------------Giv a number of column-----------------////
Console.WriteLine("Podaj kolumne");
int a = Convert.ToInt32(Console.ReadLine());
//Console.WriteLine("Podaj wiersz");
//int b = Convert.ToInt32(Console.ReadLine());
int n = tablica.Length;
int m = tablica[a].Split(',').Length;
string[,] liczby = new string[n, m];
for (int j = 0; j < n; j++)
{
int suma = 0;
for (int i = 0; i < m; i++)
{
//somethink should be here
}
}
有解决这个问题的想法吗?
尝试:
//data patch
string[] tekst = File.ReadAllLines(@"C:\zoo.txt");
//full of array - you allready have a string[], no need for a new one
//string[] tablica = tekst;
for(int s=0;s<tekst.Length;s++)
{
Console.WriteLine(tekst[s]);
}
//----------------Show all of array---------------------------//
//----------------Giv a number of column-----------------////
// try to use names with some meaning for variables, for example instead of "a" use "column" for the column
Console.WriteLine("Podaj kolumne");
int column = Convert.ToInt32(Console.ReadLine());
// your result for zeros in a given column
int suma = 0;
// for each line in your string[]
foreach ( string line in tekst )
{
// get the line separated by comas
string[] lineColumns = line.Split(',');
// check if that column is a zero, remember index is base 0
if ( lineColumns[column-1] == "0" )
suma++;
}
Console.WriteLine(suma);
编辑:只需确保验证他们要求的列确实存在于您的数组中,并且如果您不认为第一列是具有名称的列,请调整此部分
lineColumns[column] // instead of lineColumns[column-1]