C# 循环打印
我想打印这个:-C# 循环打印,c#,C#,我想打印这个:- 1. 121 12321 1234321 123454321 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int
1.
121
12321
1234321
123454321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c, d, e;
b = 6;
for(a=b;a>=1;a--)
{
for (c = a; c >=1; c--)
{
Console.Write(" ");
}
for(d=1; d<=b-a;d++)
{
Console.Write(d);
}
for (e = b-a-1; e>=a; e--)
{
Console.Write(e);
}
Console.WriteLine();
}
}
}
}
这个程序打印这个。程序中可能有什么错误?在for循环中?请告诉我如何解决这个问题。我使用的逻辑有什么错误。如果是,我的逻辑有什么问题?在不久的将来,我应该如何思考算法-
1.
12
123
123432
123454321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, c, d, e;
b = 6;
for(a=b;a>=1;a--)
{
for (c = a; c >=1; c--)
{
Console.Write(" ");
}
for(d=1; d<=b-a;d++)
{
Console.Write(d);
}
for (e = b-a-1; e>=a; e--)
{
Console.Write(e);
}
Console.WriteLine();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
INTA、b、c、d、e;
b=6;
对于(a=b;a>=1;a--)
{
对于(c=a;c>=1;c--)
{
控制台。写(“”);
}
对于(d=1;d=a;e--)
{
控制台。写入(e);
}
Console.WriteLine();
}
}
}
}
请告诉我代码中的问题。问题在(e=b-a-1;e>=a;e--)的
行中。
将(e=b-a-1;e>=1;e--)更改为简单的。
类程序
{
静态void Main(字符串[]参数)
{
int a=1,b,s=1,n=5;
对于(;a=1;b--)
{
控制台。写(“”);
}
控制台。写入(s*s);
Console.WriteLine();
a++;
}
}
}
希望这能有所帮助。@chouaib对于代码中的问题绝对正确 但是,我们需要处理您的代码以及如何编写代码 首先是变量名<代码>a、
b
、c
、d
和e
没有任何意义。通过查看和阅读这段代码,我不知道它会做什么。您希望您的代码易于阅读者理解。
使用有意义的变量名。对于这样的问题,我可能会使用行
和列
,这取决于我的解决方法
这让我想到下一件事,如何着手解决这个问题。
实际上,您已经有了一个表,可以用行和列向其中写入值。因此,一个好的开始是迭代每行的列。差不多
int totalNumberOfRows = 5;
int totalNumberOfColumns = 9;
// Iterate over each row
for (int row = 0; row < totalNumberOfRows; row++)
{
// Iterate over each column of each row
for (int column = 0; column < totalNumberOfColumns; column++)
{
// Do something
}
}
一旦你做到了这一点,你就快成功了。
如果你看着一个问题,却不知道如何解决它,试着把它分解成更小、更容易的问题
在编写代码时,请考虑让其他人易于阅读和理解。代码需要具有可维护性和可扩展性,这意味着当有人需要对代码进行修改以添加到代码中或修复错误时,他们不想花费数小时阅读代码来理解它。请解释一下!它工作正常,但我不明白!Rahul,你可以为它做一次试运行,注意n是图案中的线条数,其5非常感谢你的提示!:D
1
121
12321
etc...