方形数字模式

  • 本文关键字:模式 数字 方形 c#
  • 更新时间 :
  • 英文 :


您好,我在这个程序中遇到了困难我的代码是

using System;
class Pattern{
static void Main()
{
int rows, i, j, k;

Console.WriteLine("Enter the no. of rows: ");
rows = int.Parse(Console.ReadLine());
for (i = 1; i <= rows; i++)
{

for (j = 1; j <= rows; j++)
{
k=i+j-1;
if (k>rows){
k=k-2;
}
Console.Write(k+ " ");


}
Console.WriteLine();
}
}
}

我得到的输出是

123
232
323

我想要得到的输出是当我输入3输出应该是

123
232
321

当我输入4输出应该是

1234
2343
3432
4321

我的代码中似乎出了什么问题?谢谢

int count = int.Parse(Console.ReadLine());
for (int i = 1; i <= count; i++)
{
string s = string.Empty;
for (int j = i; j <= count + i - 1; j++)
{
int v;
if (j > count)
{
v = count - (j - count);
}
else
{
v = j;
}
s += v;
}
Console.WriteLine(s);
}
using System; class Pattern{
static void Main()
{
int rows, i;
string x = "1";

Console.WriteLine("Enter the no. of rows: ");
rows = int.Parse(Console.ReadLine());

for(i = 2; i<= rows; i++){
x = x+ i.ToString();
}
for(i = rows-1; i>= 1; i--){
x = x+ i.ToString();
}

for(i = 0; i< rows; i++){
Console.WriteLine(x.Substring(i, rows));
}

} }

您可以尝试这个解决方案,但它与使用循环进行迭代不同。你们可以尝试你们自己的解决方案,我希望我的解决方案是正确的答案。

最新更新