如何在控制台应用程序中为Fibonacci创建逻辑 [英] How Do I Create A Logic For Fibonacci In Console Application
本文介绍了如何在控制台应用程序中为Fibonacci创建逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi Team,
i的代码如下:工作正常。我想在代码中再实现一个逻辑
i想要输出如下:
0
.1
..1
... 2
.... 3
..... 5
...... 8
只是忽略点数。输出应该是这样的。
i想要数字之前的空格。我需要做什么改变。请指导我。
Hi Team,
i have the code as follows:Its working fine .i want to implement one more logic in the code
i want the output as follows:
0
.1
..1
...2
....3
.....5
......8
just ignore the dots .the output should look like this.
i want the space before the numbers .What changes i need to make .please guide me.
namespace Triangle
{
class Program
{
public static int Fibonacci(int n)
{
int a = 0;
int b = 1;
for (int i = 0; i < n; i++)
{
int temp = a;
a = b;
b = temp + b;
}
return a;
}
static void Main()
{
for (int i = 0; i < 15; i++)
{
Console.WriteLine(Fibonacci(i));
Console.ReadLine();
}
}
}
}
谢谢
harshal
thanks
harshal
推荐答案
如果我理解正确你需要改变行Console.WriteLine(Fibonacci(i));
在每个号码前显示一个空格。
你需要复合格式 [ ^ ]
例如
If I have understood you correctly you need to change the lineConsole.WriteLine(Fibonacci(i));
to display a space before each number.
You need "Composite Formatting"[^]
For example
Console.WriteLine(" {0}", Fibonacci(i));
- 忽略点使我困惑。最简单的方法是将行 Console.WriteLine(Fibonacci(i));
更改为
Console.WriteLine(Fibonacci(i).ToString().PadLeft(i + 1,' '));
class Program
{
public static int Fibonacci(int n)
{
int a = 0;
int b = 1;
for (int i = 0; i < n; i++)
{
int temp = a;
a = b;
b = temp + b;
}
return a;
}
static void Main()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < i; j++)
{
Console.Write(" ");
}
Console.WriteLine(Fibonacci(i));
Console.ReadLine();
}
}
这篇关于如何在控制台应用程序中为Fibonacci创建逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文