简体   繁体   English

Console.WriteLine无法正常工作?

[英]Console.WriteLine not working?

I've created a program which should calculate the surface area of an irregular shaped object, such as a lake. 我创建了一个程序,该程序应该计算不规则形状的物体(例如湖泊)的表面积。 I read in a file, which contained the values for the x and y values, and the depth. 我读了一个文件,其中包含x和y值以及深度。

I'm new to C#, and so I don't fully understand everything yet, but I think my code should work, however, it doesn't seem to be writing the value for the area onto the screen. 我是C#的新手,所以我还不完全了解所有内容,但是我认为我的代码应该可以工作,但是,它似乎并没有将面积的值写到屏幕上。

I know that Console.WriteLine(_surface); 我知道Console.WriteLine(_surface); SHOULD work , but it I can't seem to get it to do anything, and it's probably in the wrong place! 应该工作,但是我似乎无法使它做任何事情,而且可能在错误的位置!

Can someone please tell me where I'm going wrong? 有人可以告诉我我要去哪里了吗?

My code is as follows. 我的代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NUnit.Framework;


namespace ConsoleApplication1
{


    public class ValueXyz
    {
        public double X { get; set; }
        public double Y { get; set; }
        public int Z { get; set; }


    }


    public class SurfaceCalculator
    {

        private ValueXyz[] _valuesXyz;
        private double _surface;
        private readonly string _textWithValues;
       public SurfaceCalculator(string textWithValues)

        {
            _textWithValues = textWithValues;
            SetValuesToCalculate();
        }

        public double Surface
        {
            get { return _surface; }

        }


        public void CalculateSurface()
        {

            for (var i = 0; i < _valuesXyz.Length; i++)
            {
                if (_valuesXyz[i].Z == 0)
                    _surface = (_valuesXyz[i].X * _valuesXyz[i + 1].Y) - (_valuesXyz[i + 1].X * _valuesXyz[i].Y);

                Console.WriteLine(_surface);
            }


        }


        private void SetValuesToCalculate()
        {
            var valuesXyz = _textWithValues.Split(' ');


            _valuesXyz = valuesXyz.Select(item => new ValueXyz
            {
                X = Convert.ToDouble(item.Split(',')[0]),
                Y = Convert.ToDouble(item.Split(',')[1]),
                Z = Convert.ToInt32(item.Split(',')[2])


            }).ToArray();




        }



        public void TestSurfaceCalculatorGetsAValue()
        {

            var textWithValues = File.ReadAllLines(@"C:\Users\user\Documents\Visual Studio 2010\Projects\Lake_Take_Toooooo\Lake_Take_Toooooo\bin\Debug\Lake_Test.csv");
            var calculator = new SurfaceCalculator(_textWithValues);
            calculator.CalculateSurface();


            Assert.IsNotNull(calculator.Surface);

        }

        static void Main()
        {

            Console.ReadKey();
        }
    }
}

This is my first time using classes, so apologies if there's an obvious answer. 这是我第一次使用类,因此如果有明显的答案,我们深表歉意。

Thanks for your help! 谢谢你的帮助!

You need to actually call the method inside the Main method, which is the program entry point. 您实际上需要在Main方法(程序入口点)中调用该方法。 Like: 喜欢:

    static void Main()
    {
        CalculateSurface();
        Console.ReadKey();
    }

When you run your program, only the code inside the Main method is actually executed. 当您运行程序时,实际上仅执行Main方法中的代码。 If you do not call anything from there then no code is executed. 如果您不从那里调用任何内容,则不会执行任何代码。

Main事件中没有调用任何函数...我应该想象读键会等待键输入然后关闭,对吗?

Are you trying to run this as console application or as unit test? 您是要作为控制台应用程序还是作为单元测试来运行它? (It looks like you're trying to run it as unit test since you're using NUnit.Framework and there is a Test-method with an Assert...) (由于您使用的是NUnit.Framework,因此您似乎试图将其作为单元测试运行,并且有一个带有Assert的Test-method ...)

If you want to run it as console application, you have to call the code that should get executed in the Main method. 如果要将其作为控制台应用程序运行,则必须调用应在Main方法中执行的代码。

If you want to run it as unit test, you have to add some "attributes" to your test class and test method. 如果要将其作为单元测试运行,则必须在测试类和测试方法中添加一些“属性”。 The class should have the [TestFixture] attribute, and the method should have the [Test] attribute, like: 该类应具有[TestFixture]属性,该方法应具有[Test]属性,例如:

[TestFixture]
public class SurfaceCalculator {

...

[Test]
public void TestSurfaceCalculatorGetsAValue() {
...
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM