标题:自己在做一个方法时,出现一个错误,解决不了,求高手
只看楼主
lelejiayou
Rank: 1
来 自:宁海
等 级:新手上路
帖 子:73
专家分:0
注 册:2013-3-20
结帖率:73.68%
已结贴  问题点数:20 回复次数:7 
自己在做一个方法时,出现一个错误,解决不了,求高手
数字转货币.zip (34.89 KB)
它错误显示说,并非所有代码都有返回值,,我该怎么改。。。
2013-04-24 10:42
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
得分:5 
你这个方法可不只一个错误啊
没有return语句
if语句中我想你本意是想判断z吧,结果你用了h
int.Parse("壹") 你要转换的话类似int.Parse("1")这样写
后面我没看下去。

这种转换真要写的好可没这么简单
2013-04-24 11:53
lelejiayou
Rank: 1
来 自:宁海
等 级:新手上路
帖 子:73
专家分:0
注 册:2013-3-20
得分:0 
给点提示也可以啊,,
2013-04-24 12:07
lantian8134
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:8
帖 子:115
专家分:789
注 册:2013-1-11
得分:0 
你上面说的错误就是没有return语句造成的
2013-04-24 12:11
都市浮尘
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2013-4-24
得分:5 
在有返回值的方法里必须所有的循环 都要返回值的 仔细看一下
2013-04-24 14:16
lelejiayou
Rank: 1
来 自:宁海
等 级:新手上路
帖 子:73
专家分:0
注 册:2013-3-20
得分:0 
private static  int Bianhua(int h)
        {
           // int h=0;
            if (h == 1)
            {
                return h = int.Parse("1");
            }
            else if (h == 2)
            {
                return h = int.Parse("贰");
            }
            else if (h == 3)
            {
                return h = int.Parse("叁");
            }
            else if (h == 4)
            {
                return h = int.Parse("肆");
            }等等,改成这样还是原来那个错误哎。。
2013-04-24 22:06
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:5 
以下是引用lelejiayou在2013-4-24 22:06:31的发言:

 private static  int Bianhua(int h)
        {
           // int h=0;
            if (h == 1)
            {
                return h = int.Parse("1");
            }
            else if (h == 2)
            {
                return h = int.Parse("贰");
            }
            else if (h == 3)
            {
                return h = int.Parse("叁");
            }
            else if (h == 4)
            {
                return h = int.Parse("肆");
            }等等,改成这样还是原来那个错误哎。。

假如h == 11111或是12345什么的呢,这时候返回什么呢,或许h不会等于这些,但方法并不知道啊,你要么有else,要么就在if语句外面,再加一个return
2013-04-25 00:36
lxb932979339
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:84
专家分:313
注 册:2013-4-24
得分:5 
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!IsNum(textBox1.Text.Trim()))
            {
                MessageBox.Show("你输入的不是数字,请重新输入!!");
                textBox1.Focus();
                return;
            }
            textBox2.Text = ChangeToNew(textBox1.Text);
        }
        public bool IsNum(string str)//判断输入的字符是不是数字
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str, @"^[0-9]*$");
        }
      
        public string ChangeToChar(string str)
        {
            string ch = "零";

            switch (str)
            {
                case "0": ch = "零";
                    break;
                case "1": ch = "壹";
                    break;
                case "2": ch = "贰";
                    break;
                case "3": ch = "叁";
                    break;
                case "4": ch = "肆";
                    break;
                case "5": ch = "伍";
                    break;
                case "6": ch = "陆";
                    break;
                case "7": ch = "柒";
                    break;
                case "8": ch = "捌";
                    break;
                case "9": ch = "玖";
                    break;
            }
            return ch;
        }
        public int NumBase(int length)
        {
            int j = 10;
            for (int i = 0; i <length; i++)
            {
                if (i == 0)
                {
                    j = 1;
                }
                else if (i == 1)
                {
                    j = 10;
                }
                else
                {
                    j = j * 10;
                }
            }
            return j;
        }
        public string ChangeToNew(string str)
        {
            int NumLength = str.Length;
            StringBuilder sb = new StringBuilder();
            int numStr = 0;
            try
            {
                numStr = int.Parse(str.Trim());
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            int numTemp = 0;
            for (int i = NumLength ; i>0; i--)
            {
                int j = numStr / NumBase(i);
                numTemp = numStr;
                numStr = numTemp - j * NumBase(i);
                string g = ChangeToChar(j.ToString ());
                switch (i)
                {
                    case 0: sb.Append(g); break;
                    case 1: sb.Append(g + "元整"); break;
                    case 2: sb.Append(g+"十"); break;
                    case 3: sb.Append(g+"百"); break;
                    case 4: sb.Append(g + "千"); break;
                    case 5: sb.Append(g + "万"); break;
                    case 6: sb.Append(g + "十"); break;
                    case 7: sb.Append(g + "百"); break;
                    case 8: sb.Append(g + "千"); break;
                    case 9: sb.Append(g + "亿"); break;
                    case 10: sb.Append(g + "十"); break;
                    case 11: sb.Append(g + "百"); break;
                    case 12: sb.Append(g + "千"); break;
                    case 13: sb.Append(g + "兆"); break;

                }
            }
            return sb.ToString();

        }
    }
}
2013-04-25 00:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-405319-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.354652 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved