场景:汤姆去参加比赛。给他的任务是绘制如下指定的图案。提示用户输入数字并显示模式。
重要:保持所有方法为“公共”和“静态”
要求:
显示矩阵
矩阵的对角线填充为 0
下三角边填充-1
上三角边填充1
样本输入/输出
样本输入:1
Enter a number:
3
Sample Output :
0 1 1
-1 0 1
-1 -1 0
样本输入 2:
Enter a number
4
Sample Output :
0 1 1 1
-1 0 1 1
-1 -1 0 1
-1 -1 -1 0
下面是我正在执行的引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace C_SHARP
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int n = Convert.ToInt32(Console.ReadLine());
int[,] arr1 = new int[n,n];
GetArray(n);
for (int i = 0; i < n; i++)
{
Console.Write("\n");
for (int j = 0; j < n; j++)
{
Console.Write("{0}\t", arr1[i, j]);
}
}
Console.Write("\n\n");
}
public static int[,] GetArray(int num)
{
int i, j;
int[,] arr1 = new int[num, num];
for (i = 0; i < num; i++)
{
for (j = 0; j < num; j++)
{
if (i < j)
{
arr1[i,j]=1;
}
else if (i > j)
{
arr1[i, j] = -1;
}
else
{
arr1[i, j] = 0;
}
}
}
return arr1;
}
}
}
在执行这个程序时,它打印所有的行和列 0。 请帮我打印所需描述的矩阵。
答案 0 :(得分:4)
您返回数组,但不要将其分配给任何东西,请尝试:
var arr1 = GetArray(n);
代替:
int[,] arr1 = new int[n,n];
GetArray(n);