C# 如何在c中打印二进制数的三角形
我需要在这个代码段中添加二进制数逻辑。我只是不知道如何实现二进制数,我可以加0和1,但这似乎不对C# 如何在c中打印二进制数的三角形,c#,visual-studio,C#,Visual Studio,我需要在这个代码段中添加二进制数逻辑。我只是不知道如何实现二进制数,我可以加0和1,但这似乎不对 namespace Star_Pyramid { class Program { static void Main(string[] args) { int num; Console.WriteLine("enter level"); num = Int32.Parse(Console.ReadLine()); int
namespace Star_Pyramid
{
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("enter level");
num = Int32.Parse(Console.ReadLine());
int count = 1;
for (int lines = num; lines >= 1; lines--)
{
for (int spaces = lines - 1; spaces >= 1; spaces--)
{
Console.Write(" ");
}
for (int star = 1; star <= count; star++)
{
Console.Write("*");
Console.Write(" ");
}
count++;
Console.WriteLine();
}
Console.ReadLine();
}
}
}
你可以使用%
请解释一下你所说的加二进制数是什么意思logic@SamiKuhmonen我需要打印一个由二进制组成的三角形numbers@MohammadQasim的第一个内部可替换为Console.Writenew字符串“”,第1行;在上面提到的行中,0:1是什么;平均值?交替打印1和0。。试着看演示哦,我明白了。所以0:1;有点像Console.Write0和Console.Write1@穆罕默德·卡西姆?和:是一个条件赋值。在那之前?你看情况。之后呢?您可以看到当条件为真时将分配的值,在:之后可以看到当条件为假时将分配的值。在本例中,星%2==0是条件,结果0或1将被分配给c。在没有三元运算符语句的情况下,%2将已经返回1或0?真假
c = star % 2; // will print first the '1'
c = (star + 1) % 2; // will print first the '0'
int num;
Console.WriteLine("enter level");
num = Int32.Parse(Console.ReadLine());
int count = 1;
int c = 0;
for (int lines = num; lines >= 1; lines--)
{
for (int spaces = lines - 1; spaces >= 1; spaces--)
{
Console.Write(" ");
}
for (int star = 1; star <= count; star++)
{
c = star % 2; //this will return 1 if the value of star is odd then 0 if even
Console.Write(c);
Console.Write(" ");
}
count++;
Console.WriteLine();
}
Console.ReadLine();