C# 创建由星号组成的多维数组
我是编程新手,我希望我的程序运行一个由星星(星号)组成的表。例如,表4x3有4x3颗星。但我最初的问题是,我不知道如何实现多维数组,我只需要更改行和列的初始值,以创建更多或更少的星星 所以:这是我目前的代码:C# 创建由星号组成的多维数组,c#,arrays,multidimensional-array,dynamic,C#,Arrays,Multidimensional Array,Dynamic,我是编程新手,我希望我的程序运行一个由星星(星号)组成的表。例如,表4x3有4x3颗星。但我最初的问题是,我不知道如何实现多维数组,我只需要更改行和列的初始值,以创建更多或更少的星星 所以:这是我目前的代码: using System; namespace ConsoleApplication1 { class Multidimensional_array { static void Main(string[] args) {
using System;
namespace ConsoleApplication1
{
class Multidimensional_array
{
static void Main(string[] args)
{
int arrayRows = 4;
int arrayCols = 3;
int[,] arrayTimes;
arrayTimes = new int [arrayRows, arrayCols];
//String star = "*";
for( int i = 0; i <= arrayRows; i++) {
for( int j = 0; j <= arrayCols; j++) {
//Console.WriteLine("*");
//arrayTimes[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.ReadLine();
}
}
}
使用系统;
命名空间控制台应用程序1
{
类多维数组
{
静态void Main(字符串[]参数)
{
int arrayRows=4;
int-arrayCols=3;
int[,]数组;
arrayTimes=新整数[arrayRows,arrayCols];
//字符串星=“*”;
对于(int i=0;i我认为您非常接近,您只是为数组使用了错误的数据类型,没有将*
分配给所述数组中的每个位置,并且您当前的代码将给您一个ArrayIndexOutOfBounds
异常。您可能有4行3列,但数组索引是零基的,这意味着当您访问pos时条件1、2、3等。分别使用0、1、2等索引
因此,既然您想存储文本“*”,那么您应该为多维数组使用char[,]
或string[,]
。为此,我选择了char[,]
:
int arrayRows = 4;
int arrayCols = 3;
char[,] arrayTimes = new char[arrayRows, arrayCols];
const char star = '*';
// Set it up
for (int i = 0; i <= arrayRows - 1; i++)
{
for (int j = 0; j <= arrayCols - 1; j++)
{
arrayTimes[i, j] = star;
}
}
// Print it out
for (int i = 0; i <= arrayRows - 1; i++)
{
for (int j = 0; j <= arrayCols - 1; j++)
{
Console.Write(arrayTimes[i, j]);
}
Console.WriteLine();
}
int-arrayRows=4;
int-arrayCols=3;
char[,]arrayTimes=新的char[arrayRows,arrayCols];
常量字符星='*';
//设置它
对于(int i=0;i简单解
static void Main(string[] args)
{
// Get the number of rows
Console.WriteLine("Enter the number of rows:");
int arrayRows = Convert.ToInt32(Console.ReadLine());
// Get the number of columns
Console.WriteLine("Enter the number of columns:");
int arrayCols = Convert.ToInt32(Console.ReadLine());
// For each item in the row
for (int i = 0; i < arrayRows; i++)
{
// For each item in the column
for (int j = 0; j < arrayCols; j++)
{
// Show a star
Console.Write("*");
}
// End the line
Console.WriteLine("");
}
// Read the line to stop the app from closing
Console.ReadLine();
}
static void Main(字符串[]args)
{
//获取行数
Console.WriteLine(“输入行数:”);
int arrayRows=Convert.ToInt32(Console.ReadLine());
//获取列数
Console.WriteLine(“输入列数:”);
int arrayCols=Convert.ToInt32(Console.ReadLine());
//对于行中的每个项目
for(int i=0;i
如果你想储存星星
static void Main(string[] args)
{
// Get the number of rows
Console.WriteLine("Enter the number of rows:");
int arrayRows = Convert.ToInt32(Console.ReadLine());
// Get the number of columns
Console.WriteLine("Enter the number of columns:");
int arrayCols = Convert.ToInt32(Console.ReadLine());
// Create an array
char[,] arrayTimes = new char[arrayRows, arrayCols];
char star = '*';
// For each item in the row
for (int i = 0; i < arrayRows; i++)
{
// For each item in the column
for (int j = 0; j < arrayCols; j++)
{
// Show a star
arrayTimes[i, j] = star;
}
}
// Read the line to stop the app from closing
Console.ReadLine();
}
static void Main(字符串[]args)
{
//获取行数
Console.WriteLine(“输入行数:”);
int arrayRows=Convert.ToInt32(Console.ReadLine());
//获取列数
Console.WriteLine(“输入列数:”);
int arrayCols=Convert.ToInt32(Console.ReadLine());
//创建一个数组
char[,]arrayTimes=新的char[arrayRows,arrayCols];
字符星='*';
//对于行中的每个项目
for(int i=0;i
这就是你想要的
int arrayRows = 2;
int arrayCols = 2;
char[,] arrayTimes;
arrayTimes = new char[arrayRows, arrayCols];
//String star = "*";
for (int i = 0; i < arrayRows; i++)
{
for (int j = 0; j < arrayCols; j++)
{
arrayTimes[i, j] = '*';
Console.Write("{0}",arrayTimes[i, j]);
}
Console.WriteLine();
}
Console.ReadKey();
int-arrayRows=2;
int-arrayCols=2;
字符[,]数组;
arrayTimes=新字符[arrayRows,arrayCols];
//字符串星=“*”;
for(int i=0;i
C#中的下标是基于零的,即当您创建一个由4个元素组成的数组时,下标的范围是0到3。是的,是的……非常感谢您,先生。我不确定您为什么必须初始化“const char”。这对程序员来说是个好做法吗?老实说,我也不太了解const的功能。const
是constant
的缩写。一个非常简单的解释是,如果您有一个变量,您不想在定义后更改它,那么您可以在它的声明前面加上const
例如,如果在两个for循环之间,您试图编写star='#'
,编译器将不允许这样做。我非常接近,哈哈…非常感谢您提供了简短的答案。是的,您非常接近。我们大多数人都像这样坚持下去…祝您好运