A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李玲 中级黑马   /  2012-6-11 10:35  /  1910 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用C#如何将数字显示改为文件显示,也就是a为1,则显示ONE,a为6,则显示SIX,a为11,则显示ELEVEN,一直显示到100为止。
还请各位能够指点一下。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

8 个回复

倒序浏览
可以建立一个字符串数组啊,顺序存放ONE,TWO,THERE......,a作为下标,每次输出a对应的字符串就可以了。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
  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.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
方法很多种  
1 、建立两个对应的字符串数组,同上
2、switch语句,不过效率低
3、可以用范型容器,根据Key取出Value

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
方法很多,可以用字典、哈希表和枚举

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
string[] enumber={"one","two"....};

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

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

console.readkey();
没测试,不知道对不对

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
方法有很多种的,可以用数组:如:{“one”,"two","three"……}用下标取值。
可以用枚举 public enum date{one,two,three,……}
可以用字典,字典要用key来取出对应的value

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
可以用字符串数组、枚举、哈希表、字典,但是对应的one、two这些还得你自己敲出来的~!

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
你需要转换的数字比较大,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.     }
复制代码

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马