📅  最后修改于: 2020-10-31 14:22:48             🧑  作者: Mango
在C#语言中,我们可以借助循环和切换大小写轻松地将数字转换为字符。在此程序中,我们从用户那里获取输入并对该数字进行迭代,直到它为0。在迭代过程中,我们将其除以10,然后将其余部分传递给switch大小写,以获得该数字的单词。
让我们看一下C#程序将字符转换为数字。
using System;
public class ConversionExample
{
public static void Main(string[] args)
{
int n,sum=0,r;
Console.Write("Enter the Number= ");
n= int.Parse(Console.ReadLine());
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
n=sum;
while(n>0)
{
r=n%10;
switch(r)
{
case 1:
Console.Write("one ");
break;
case 2:
Console.Write("two ");
break;
case 3:
Console.Write("three ");
break;
case 4:
Console.Write("four ");
break;
case 5:
Console.Write("five ");
break;
case 6:
Console.Write("six ");
break;
case 7:
Console.Write("seven ");
break;
case 8:
Console.Write("eight ");
break;
case 9:
Console.Write("nine ");
break;
case 0:
Console.Write("zero ");
break;
default:
Console.Write("tttt ");
break;
}//end of switch
n=n/10;
}//end of while loop
}
}
输出:
Enter the Number= 357546
three five seven five four six