我如何摆脱stackoverflowexception [英] How do I get rid of the stackoverflowexception

查看:65
本文介绍了我如何摆脱stackoverflowexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个领域从未使用过,不确定为什么? - > private int realValue和private int imaginaryValue



我尝试过:



The field is never used not sure why? --> private int realValue and private int imaginaryValue

What I have tried:

using System;

class Complex
    { 

    public class ComplexNum
{
    private int realValue;
    private int imaginaryValue;

}

   public Complex(int realPart, int imaginaryPart)
        {
           this.realValue = realPart;
            this.imaginaryValue = imaginaryPart;
        
        }

    public Complex()
    {
        this.realValue = 0;
       this. imaginaryValue = 0;
}

        public int realValue
        {
            get
                { return realValue; }
            set
                { realValue = value; }

        }

        public int imaginaryValue
        {
            get
               { return imaginaryValue; }
            set
                { imaginaryValue = value; }
         }

        public override string ToString()
        {
    return string.Format(" ({0}, {1})", realValue, imaginaryValue);

            }

    public void add(Complex a, Complex b)
{
    this.realValue = a.realValue + b.realValue;
    this.imaginaryValue = a.imaginaryValue + b.imaginaryValue;

}
    public void subtract(Complex a, Complex b)
{
    this.realValue = a.realValue + b.realValue;
    this.imaginaryValue = a.imaginaryValue + b.imaginaryValue;
        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();
}
   
    }

    public class ComplexTest
        {
            
    public static void Main(string[] args)
    {
        Console.WriteLine("Complex Numbers\n");

        Complex a = new Complex(6, 8);
        Complex b = new Complex(10, 18);

        Complex result1 = new Complex();
        Complex result2 = new Complex();

        Console.WriteLine("1st Number:");
        Console.WriteLine(a.ToString());
        Console.WriteLine("2nd Number:");
        Console.WriteLine(b.ToString());

        result1.add(a, b);
        Console.Write("\nTest Addition:\nAfter Adding:");
        Console.WriteLine(result1.ToString());
        Console.ReadLine();


        result2.subtract(a, b);

        Console.Write("\nTest Subtraction:\nAfter Subtracting: ");
        Console.WriteLine(result2.ToString());
        Console.ReadLine();

        Console.WriteLine("Press enter to exit.");
        Console.ReadLine();




    }
}

推荐答案

添加减去看起来非常相似。

-----

你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:运行你的代码在调试器上一步一步地检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。

那里在调试器中没有魔力,它没有找到bug,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio中的调试2010 - 初学者指南 [ ^ ]



此解决方案的缺点:

- 这是一个DIY,你是一个跟踪问题并找到根源,这将导致解决方案。

此解决方案的优点:

- 您看到您的代码行为,您将其与您的期望相匹配。



次要效果

- 你会为自己找到虫子感到骄傲。

- 你技能会提高。



你应该很快就会发现错误。
add and subtract are looking incredibly the same.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behavior, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.


这篇关于我如何摆脱stackoverflowexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆