首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

大家帮小弟我改进上代码

2012-07-26 
大家帮我改进下代码!做了一个小程序,没有用面向对象,采用的面向过程。刚测试的时候发现2个问题:1.数组里面

大家帮我改进下代码!
做了一个小程序,没有用面向对象,采用的面向过程。刚测试的时候发现2个问题:1.数组里面的钱如sql[0,2]不能用sql[i.sql.GetLength(1)-1]来表示这样如果我再往数组里面写数据的话那又要加个判断账号,然后读取里面的钱!这是一处!
2.如果反复取钱,存钱的话,他都显示的是只存或者只取一次的结果,怎么改进了!
提前谢谢大家了,祝大家新年快乐!
由于字数超过了限制,我一个问题截取一个部分!

C# code
public void AutoTakeMoney()        {            int[,] sql = new int[3, 3] {{123,456,5000},{789,132,1800},{159,753,3000}};            Console.WriteLine("欢迎使用建设银行提款机");            start_1:            Console.WriteLine("请输入您的账号");            int username = int.Parse(Console.ReadLine());            //验证账号            for (int i = 0; i < sql.GetLength(0); i++)            {                if (username == sql[i,0])                {                    Console.WriteLine("账号正确");                    goto start2;                    //break;                }                else                {                    if (i == sql.GetLength(0) - 1)                    {                        Console.WriteLine("账号错误");                        goto start_1;                    }                }            }            //验证密码            start2:            Console.WriteLine("请输入您的密码");            int password = int.Parse(Console.ReadLine());            int count = 0;            for (int i = 0; i < sql.GetLength(0); i++)            {                for (int k = 0; k < sql.GetLength(1); k++)                {                    if (password == sql[i, k] && username == sql[i, 0])                    {                        Console.WriteLine("密码正确");                        goto start3;                        //return;                    }                    else                    {                        if (k == sql.GetLength(1)-1&&i==sql.GetLength(0)-1)                        {                            Console.WriteLine("密码错误");                            count++;                            if (count > 3)                            {                                Console.WriteLine("密码输入错误超过3次,请取卡");                            }                            //return;                            goto start2;                                                    }                                          }                }            }        start3:            Console.WriteLine("请选择你要进行的操作:1.查询余额\t2.存款\t3.取款\t4.取卡");            int operate = int.Parse(Console.ReadLine());            switch (operate)            {                case 1: if (username == 123)                    {                        Console.WriteLine("还有{0}个大洋", sql[0, 2]);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                                           }                    if (username == 789)                    {                        Console.WriteLine("还有{0}个大洋", sql[1, 2]);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                    }                    if (username == 159)                    {                        Console.WriteLine("还有{0}个大洋", sql[2, 2]);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                    }                     break;                case 2: if (username==123)                    {                        Console.WriteLine("请输入您要存的钱数");                        double money = double.Parse(Console.ReadLine());                        double nowmoney = sql[0, 2] + money;                        Console.WriteLine("好啦,帮您存进去了,现在卡里有{0}元", nowmoney);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                    }                    if (username == 789)                    {                        Console.WriteLine("请输入您要存的钱数");                        double money = double.Parse(Console.ReadLine());                        double nowmoney = sql[1, 2] + money;                        Console.WriteLine("好啦,帮您存进去了,现在卡里有{0}元", nowmoney);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                    }                    if (username == 159)                    {                        Console.WriteLine("请输入您要存的钱数");                        double money = double.Parse(Console.ReadLine());                        double nowmoney = sql[0, 2] + money;                        Console.WriteLine("好啦,帮您存进去了,现在卡里有{0}元", nowmoney);                        Console.WriteLine("还要继续吗?Y or N");                        string choose = Console.ReadLine().ToUpper();                        if (choose == "Y")                        {                            Console.Clear();                            goto start3;                        }                        else                        {                            Console.WriteLine("不继续就取回卡片");                            return;                        }                    }                     break; 



[解决办法]
程序没看,光看了一下你的问题,我想你可以用arraylist来处理你的数组会好点。因为在银行的帐号里,每次的存取都是一条记录,在每一条记录里都可以计算出余额,那么最后一条记录就是你当前的余额。所以用arraylist来操作记录就不会出现上面你所说的问题。希望这个回答对你有用。

热点排行