【咳咳】注意了,这个也是一道笔试题.今天新安装了VS2005,拿它开刀,竟然发现不是想像中那么简单.
【分析】
①用if...else.........是编程的吗?你
②用swtch,我当初就这些写的,常规方法,结果笔试失败!!
③刚刚想到的,用枚举+for
④................还没想到,期待你的发散思维
【思路】
枚举类型学编程的都学过,但感觉不是很常用,以至于我们都忽略了它的存在,今天终于轮到它表现了.
用两个枚举类型,把1,2,3转化为壹,贰,叁 另一个存放{圆,拾,佰.....}
通过for语句判断具体的单位
【处理的问题及总结】
①定义枚举类(下个帖子具体分析一下其中的一个Bug)
②字符串相连 【+=】 具体是怎么连?
string s="AX";
s+="zhz"; //等价于 s=s+"zhz"; 而非 s="zhz"+s
基础不扎实啊,..惭愧...
③字符转化为数字问题,头一次遇到.
从下图,我们可以清楚的看出:
Convert.ToInt32()不会转化字符,或者说它把字符转化为了ASCII码中对应的号,,,但是在开发环境中提示可以有char类型的参数,因为程序中用到了char类型的参数,才偶然发现这个问题
【源码】
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
12
/*只做简单实现,没有对输入进行验证是否合法(非法字符/长度等)*/
13
public partial class _Default : System.Web.UI.Page
14
{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
}
18
//定义一个枚举类型:数字
19
enum Number
20
{
21
零,壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖
22
}
23
//定义一个枚举类型:单位
24
enum UnitPrice_AX_Made_In_China
25
{
26
圆,拾,佰,仟,万
27
}
28
protected void btnSubmit_Click(object sender, EventArgs e)
29
{
30
//把输入的数字打散(txtNumber是一个文本框,用来输入数字)
31
char[] n =(this.txtNumber.Text).ToCharArray();
32
//用来存放显示结果
33
string show = "";
34
for (int i = 0; i <n.Length; i++)
35
{
36
//show+=k 等价于 show=show+k ,不是我想要的字符串连接
37
show = ((Number)(Convert.ToInt32(n[n.Length - i-1].ToString()))).ToString()+((UnitPrice_AX_Made_In_China)i).ToString()+show;
38
}
39
//lbShow是一个Lable,用来输出结果
40
this.lbShow.Text=show;
41
}
42
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42
