![](/img/trans.png)
[英]Increment a string from numbers 0-9 to lowercase a-z to uppercase A-Z in C#
[英]Printing the letters from A-Z using recursion in c#
所以這就是我想要的,我正在嘗試從AZ打印所有數字,但只打印Z,請幫助和謝謝(使用遞歸)
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace AtoZRecursion
{
class Program
{
static void Main(string[] args)
{
int number=65;
getAplha(number);
Console.WriteLine(Convert.ToChar(getAplha(number)));
Console.ReadKey();
}
public static int getAplha(int number=65)
{
if (number==90)
{
return Convert.ToChar(number);
}
return Convert.ToChar(getAplha(number + 1));
}
}
}
從Main
除去WriteLine
並將其放在getAlpha
,這樣就可以打印每個字母,因為每個字母都有一個調用。
您可以更改方法的返回類型並像Console.WriteLine(getAplha(65));
一樣調用它Console.WriteLine(getAplha(65));
public static string getAplha(int number = 65)
{
if (number == 90)
{
return "" + (char)number;
}
return (char)number + getAplha(number + 1);
}
當您從最深的遞歸級別“彈出”時,WriteLine僅發生一次。 您需要從getAlpha方法編寫。
您僅在Console.WriteLine
記錄遞歸的最后一個值。 相反,請像這樣包裝您的WriteLine:
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace AtoZRecursion
{
class Program
{
static void Main(string[] args)
{
int number=65;
getAplha(number);
Console.ReadKey();
}
public static int getAplha(int number=65)
{
if (number==90)
{
Console.WriteLine(Convert.ToChar(number));
return Convert.ToChar(number);
}
Console.WriteLine(Convert.ToChar(number));
return Convert.ToChar(getAplha(number + 1));
}
}
}
為此,您需要在遞歸方法內部使用Console.WriteLine
public static void getAplha(int number=65)
{
Console.WriteLine(Convert.ToChar(number));
if (number==90)
{
return;
}
getAplha(number + 1);
}
然后,您不需要返回類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.