我想根据行数和列数打印给定列表,但始终显示第6个数字
static void Main(string[] args)
{
//string total, rows, columns = "";
//Console.WriteLine("Enter Total No Of Numbers");
//total = Console.ReadLine();
//Console.WriteLine("Enter all Numbers seperated by Comma");
//var allNumbers = Console.ReadLine();
//var array = allNumbers.Split(',');
//Console.WriteLine("Enter Number of Rows");
//rows = Console.ReadLine();
//Console.WriteLine("Enter Number of columns");
//columns = Console.ReadLine();
string total, rows, columns = "";
total = "30";
var allNumbers = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";
var array = allNumbers.Split(',');
rows = "5";
columns = "6";
for (int i = 0; i < Convert.ToInt32(rows); i++)
{
for (int j = 0; j < Convert.ToInt32(columns); j++)
{
Console.Write(array[j]);
Console.Write("\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
答案 0 :(得分:1)
你走了:
string total, rows, columns = "";
total = "30";
var allNumbers = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30";
var array = allNumbers.Split(',');
rows = "5";
columns = "6";
for (int i = 0; i < Convert.ToInt32(rows); i++)
{
for (int j = 0; j < Convert.ToInt32(columns); j++)
{
Console.Write(array[j + (i * Convert.ToInt32(columns))]);
Console.Write("\t");
}
Console.WriteLine();
}
基本上在你的循环中,你从来没有尝试过索引的第一行。我认为需要做出一些评论: