C# 类方法不使用类变量

C# 类方法不使用类变量,c#,C#,我正在做一些编码练习来自学C#,现在我已经被困了好几天,试图计算其他行星上的不同时间段,是时候求助了。我至少已经消除了错误,并且它开始返回一些东西,但现在就我而言,我不明白为什么seconds不能以稳定的方式存储,然后调用方法。它只是返回零。请参阅下面的代码 public class SpaceAge { public long seconds; public SpaceAge(long seconds) {

我正在做一些编码练习来自学C#,现在我已经被困了好几天,试图计算其他行星上的不同时间段,是时候求助了。我至少已经消除了错误,并且它开始返回一些东西,但现在就我而言,我不明白为什么
seconds
不能以稳定的方式存储,然后调用方法。它只是返回零。请参阅下面的代码

    public class SpaceAge
    {
        public long seconds;

        public SpaceAge(long seconds)
        {
            Console.WriteLine("Space Age in Seconds:" + seconds); 
        }

        public double OnEarth()
        {
            double result = seconds / 31557600;
            return result;
        }
        public double OnMercury()
        {
            double result = seconds * 0.2408467;
            return result;
        }
    }  

    class Program
    {
        public static void Main()
        {
             Console.WriteLine("**Main Function Executing**");
             var age = new SpaceAge(10000000000);
             Console.WriteLine("Earth years:" + age.OnEarth());
             Console.WriteLine("Mercury years:" + age.OnMercury());       
        }
    }
它返回:

BBs-iMac:space-age bb$ dotnet run
**Main function executing**
Space Age in Seconds:10000000000
Earth years:0
Mercury years:0

构造函数有两个不同的变量,名为
seconds
:类成员和参数。您需要这样做:

public SpaceAge(long seconds)
{
    this.seconds = seconds;
    Console.WriteLine("Space Age in Seconds:" + seconds); 
}
此外,
OnEarth()
中的算术运算完全在整数空间中进行,这意味着结果的任何小数部分都将被截断。您需要确保除法运算的至少一侧为浮点类型:

public class SpaceAge
{
    public long seconds;

    public SpaceAge(long seconds)
    {
        this.seconds = seconds;
        Console.WriteLine("Space Age in Seconds:" + seconds); 
    }

    public double OnEarth()
    {
        //the "D" at the end of the number means it is a double, not an int.
        return seconds / 31557600D;
    }
    public double OnMercury()
    {
        return seconds * 0.2408467D;
    }
}  

构造函数有两个不同的变量,名为
seconds
:类成员和参数。您需要这样做:

public SpaceAge(long seconds)
{
    this.seconds = seconds;
    Console.WriteLine("Space Age in Seconds:" + seconds); 
}
此外,
OnEarth()
中的算术运算完全在整数空间中进行,这意味着结果的任何小数部分都将被截断。您需要确保除法运算的至少一侧为浮点类型:

public class SpaceAge
{
    public long seconds;

    public SpaceAge(long seconds)
    {
        this.seconds = seconds;
        Console.WriteLine("Space Age in Seconds:" + seconds); 
    }

    public double OnEarth()
    {
        //the "D" at the end of the number means it is a double, not an int.
        return seconds / 31557600D;
    }
    public double OnMercury()
    {
        return seconds * 0.2408467D;
    }
}  

你没有初始化你的字段。另外,由于
seconds
长的
,所以在除数上应该使用
D
后缀

using System;

public class SpaceAge
{
    public long seconds;
    public SpaceAge(long seconds)
    {
        this.seconds = seconds; // missing
        Console.WriteLine("Space Age in Seconds:" + seconds);
    }

    public double OnEarth()
    {
        double result = seconds / 31557600D; // add an 'D'
        return result;
    }

    public double OnMercury()
    {
        double result = seconds * 0.2408467D; // add an 'D'
        return result;
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("**Main Function Executing**");
        var age = new SpaceAge(10000000000);
        Console.WriteLine("Earth years:" + age.OnEarth());
        Console.WriteLine("Mercury years:" + age.OnMercury());
    }
}
输出:

没有“D”后缀

**Main Function Executing**
Space Age in Seconds:10000000000
Earth years:316
Mercury years:2408467000
带“D”后缀:

**Main Function Executing**
Space Age in Seconds:10000000000
Earth years:316.88087814029
Mercury years:2408466935.15778

你没有初始化你的字段。另外,由于
seconds
长的
,所以在除数上应该使用
D
后缀

using System;

public class SpaceAge
{
    public long seconds;
    public SpaceAge(long seconds)
    {
        this.seconds = seconds; // missing
        Console.WriteLine("Space Age in Seconds:" + seconds);
    }

    public double OnEarth()
    {
        double result = seconds / 31557600D; // add an 'D'
        return result;
    }

    public double OnMercury()
    {
        double result = seconds * 0.2408467D; // add an 'D'
        return result;
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("**Main Function Executing**");
        var age = new SpaceAge(10000000000);
        Console.WriteLine("Earth years:" + age.OnEarth());
        Console.WriteLine("Mercury years:" + age.OnMercury());
    }
}
输出:

没有“D”后缀

**Main Function Executing**
Space Age in Seconds:10000000000
Earth years:316
Mercury years:2408467000
带“D”后缀:

**Main Function Executing**
Space Age in Seconds:10000000000
Earth years:316.88087814029
Mercury years:2408466935.15778

你从来没有在你的
SpaceAge
构造函数中设置过
seconds
。你从来没有在
SpaceAge
构造函数中设置过
seconds
。“D”可能更好,因为他想要一个双重结果。感谢你把所有的部分放在一起。“D”可能更好,因为他想要双倍的结果。谢谢你把所有的部分放在一起。