使用递归方法调用时运行时错误(由于stackOverflowException导致进程终止)

时间:2013-09-17 11:49:30

标签: c# exception recursion

使用递归方法调用时遇到问题 我知道这里的问题setInfo(ref name, ref age, ref address, ref country);,但我不知道如何修复它。

class person{
        private string name;
        private short age;
        private string address;
        private string country;

        public person(){
            Console.Write("Hi i'm constructor \n\n");
        }

        public void setInfo(ref string name, ref short age, ref string address, ref string country){
            if (name != "" || address != "" || country != "" || age != 0) {
                this.name = name;
                this.age = age;
                this.address = address;
                this.country = country;
            } else {
                setInfo(ref name, ref age, ref address, ref country); // Here is what I doubt.
            }
        }

        public void getInfo(){
            Console.Clear();
            Console.WriteLine("---- The information ----\nName: {0}\nAge: {1}\nAddress: {2}\nCountry: {3}", this.name, this.age, this.address, this.country);
        }

    }


// When usage


static void Main(string[] args){
            string name, address, country;
            short age; 

            person one = new person();

            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.Write("Age: ");
            Int16.TryParse(Console.ReadLine(), out age);

            Console.Write("Address: ");
            address = Console.ReadLine();

            Console.Write("Country: ");
            country = Console.ReadLine();

            one.setInfo(ref name, ref age, ref address, ref country);
            one.getInfo();
        }

2 个答案:

答案 0 :(得分:1)

setInfo中检查值是否为“”,如果是,则再次调用方法而不进行任何更改。

在这种情况下,你反复调用setInfo直到堆栈已满,然后抛出异常

如果您要致电setInfo,则需要更改值...或者您只是无限次拨打setInfo

例如,

可能会给出默认值:

    public void setInfo(ref string name, ref short age, ref string address, ref string country){
            this.name    = name.Equals("")    == false? name : "abe";
            this.address = address.Equals("") == false ? address : "hevean";
            this.country = country.Equals("") == false ? country : "USA";
            this.age     = age > 0 ? age : 18;
    }

答案 1 :(得分:0)

根据您的代码,您不希望在那里进行递归。

你得到堆栈溢出,因为你在不改变任何数据的情况下进行递归调用,这就是首先导致递归调用的原因。您需要允许此人重新输入缺失的数据。那有意义吗?

尝试使用do / while循环包装输入。这有效:

    static void Main(string[] args)
    {
        string name, address, country;
        short age;

        person one = new person();

        do
        {
            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.Write("Age: ");
            Int16.TryParse(Console.ReadLine(), out age);

            Console.Write("Address: ");
            address = Console.ReadLine();

            Console.Write("Country: ");
            country = Console.ReadLine();
        } while (name == "" || address == "" || country == "" || age < 1);

        one.setInfo(ref name, ref age, ref address, ref country);
        one.getInfo();

        Console.ReadKey();
    }

几点:

1)您可能想要删除引用,因为您没有更改setInfo中的任何数据。使用ref的唯一原因是将更改后的值返回给调用者。

2)您应该创建一个构造函数,它可以在收集数据后获取所有四个值并构建您的人员。

希望有所帮助。欢呼声。