黑马程序员技术交流社区

标题: C#中转换问题 [打印本页]

作者: 李玲    时间: 2012-6-11 10:35
标题: C#中转换问题
用C#如何将数字显示改为文件显示,也就是a为1,则显示ONE,a为6,则显示SIX,a为11,则显示ELEVEN,一直显示到100为止。
还请各位能够指点一下。
作者: 刘豪    时间: 2012-6-11 10:40
可以建立一个字符串数组啊,顺序存放ONE,TWO,THERE......,a作为下标,每次输出a对应的字符串就可以了。
作者: 黑马龙超    时间: 2012-6-11 12:11
  1.         static string Function(int a)
  2.         {
  3.             string[] strs = { "one","two","three","four","five","six","seven","eight","nine","ten"};
  4.             return string.Format(strs[a-1]);
  5.         }
复制代码

作者: 吴伟烈    时间: 2012-6-19 21:29
方法很多种  
1 、建立两个对应的字符串数组,同上
2、switch语句,不过效率低
3、可以用范型容器,根据Key取出Value
作者: 戴伟    时间: 2012-6-19 23:50
方法很多,可以用字典、哈希表和枚举
作者: 王志波    时间: 2012-6-25 19:24
string[] enumber={"one","two"....};

int a=convert.toint32(console.readine());

console.writeline(string.format("您输入了{0}",enumber[a-1]));

console.readkey();
没测试,不知道对不对
作者: xiaozehope    时间: 2012-6-30 22:20
方法有很多种的,可以用数组:如:{“one”,"two","three"……}用下标取值。
可以用枚举 public enum date{one,two,three,……}
可以用字典,字典要用key来取出对应的value

作者: 戴伟    时间: 2012-7-1 10:03
可以用字符串数组、枚举、哈希表、字典,但是对应的one、two这些还得你自己敲出来的~!
作者: 古古头    时间: 2012-7-3 16:13
你需要转换的数字比较大,100以内的,不能全凭手写,我给你写一个静态类,作为工具类吧。能够装换所有的数字。
  1.     public static class NumToStr
  2.     {
  3.         private static readonly string MINUS = "minus";
  4.         private static readonly string[] X_TEN =
  5.         {
  6.             "one", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"
  7.         };
  8.         private static readonly string[] DIGIT_STR = { "", "thousand", "million", "billion" };
  9.         private static readonly int[] DIGIT_NUM = { 1, 1000, 1000000, 1000000000 };
  10.         private static readonly string[] BASE =
  11.         {
  12.             "zero","one","two","three","four","five","six","seven","eight","nine","ten",
  13.             "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"
  14.         };
  15.         private static string LessThan1000(int num)
  16.         {
  17.             String result = "";
  18.         Lable_Begin:
  19.             if (num < 21)
  20.             {
  21.                 result += BASE[num];
  22.             }
  23.             else if (num < 100)
  24.             {
  25.                 result += X_TEN[num / 10];
  26.                 if ((num %= 10) != 0)
  27.                 {
  28.                     result += "-";
  29.                     goto Lable_Begin;
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 result += BASE[num / 100] + " " + X_TEN[10];
  35.                 if ((num %= 100) != 0)
  36.                 {
  37.                     result += " and ";
  38.                     goto Lable_Begin;
  39.                 }
  40.             }
  41.             return result;
  42.         }
  43.         public static string Convert(int num)
  44.         {
  45.             if (num == 0) return BASE[0];
  46.             int abs = Math.Abs(num);
  47.             StringBuilder result = new StringBuilder(16);
  48.             for (int i = DIGIT_NUM.Length - 1; i > -1; i--)
  49.             {
  50.                 int l = abs / DIGIT_NUM[i];
  51.                 if (l > 0)
  52.                 {
  53.                     if (result.Length > 0) result.Append(' ');
  54.                     result.Append(LessThan1000(l));
  55.                     if (DIGIT_STR[i] != String.Empty) result.Append(' ');
  56.                     result.Append(DIGIT_STR[i]);
  57.                 }
  58.                 abs %= DIGIT_NUM[i];
  59.             }
  60.             return (num < 0 ? MINUS + " " : "") + result.ToString();
  61.         }
  62.     }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2