繁体   English   中英

C# 如何更改静态类的值?

[英]C# How can I change the value from static class?

所以这是程序cs。 我想在我的 StaticClass.cs 中更改 int 的值 ** 我想打印一个打印句“你希望我生成多长时间”然后更改 StaticClass 中 N 的值。

class Program
{
    

    static void Main(string[] args)
    {
        Constructor ct = new Constructor(StaticClass.n);
        //ct.xmethod(StaticClass.n);
        Console.ReadKey();
    }
}

这是我的构造函数类。 帕斯卡三角法

class Constructor
{
    
    int[][] triangle = new int[StaticClass.n][];

    public Constructor(int n)
    {
        xmethod(n);
    }

    public void xmethod(int n)
    {
        for (int row = 0; row < triangle.Length; row++)
        {

            //Each row is a subarray of length 1 greater than row number
            triangle[row] = new int[row + 1];
            //set the values in a row

            for (int k = 0; k < triangle[row].Length; k++)
            {

                // if not first or last element in the row


                if (k > 0 & k < triangle[row].Length - 1)
                    //set it to sum of the element above it
                    //and the one above and to the left
                    triangle[row][k] = triangle[row - 1][k - 1] + triangle[row - 1][k];

                //otherwise this is an end point, set it to 1
                else


                    triangle[row][k] = 1;

                //display value
                Console.Write(triangle[row][k] + "\t");
            }
            // Finished processing row-send cursor to next line
            Console.WriteLine();
            
        }
    }
}

这里我的静态类很长帕斯卡三角形

class StaticClass
{
    public const int n = 15;

    // I want to change the value of N using the Console Writeline. 

    

}

您不能更改 const 的值。 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants

请改用静态属性。

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

static class StaticClass
{
    public static int n = 15;

    // I want to change the value of N using the Console Writeline. 
}

n需要是可更新的,然后使用static代替const 但是Console.WriteLine()不是您用来更改值的东西。 你需要更清楚你想要做什么。 您是否希望有人使用 Console.ReadLine 输入n的值?

所以......从我收集的信息来看——我认为你想改变你的Main来写你的问题:“你希望我生成多长时间”然后阅读用户输入的行。 然后打印你的三角形。 就像是 :

    static void Main(string[] args)
    {
        Console.WriteLine("How long do you want me to generate? ");
        int inNum = int.Parse(Console.ReadLine());
        Constructor ct = new Constructor(inNum);
        Console.ReadKey();
    }

然后从我所看到的......你的Constructor需要改变:

    public Constructor(int n)
    {
        triangle = new int[n][];
        xmethod(n);
    }

将您的公共 const 字段改为公共静态属性:

public static int N { get; set; } = 15;

改成这样

StaticClass.N = 16;

你说“用 WriteLine 改变它” - WriteLine 输出东西,它不会改变东西。 也许你的意思是 ReadLine:

Console.WriteLine("How many levels? ");
string input = Console.ReadLine();
StaticClass.N = int.Parse(input);

请为 StaticClass 找一个更好的名字; 类应该根据它们在更真实世界的意义上代表的东西(比如..配置?)而不是它们在 C# 意义上的名称(是的,它是一个静态类,但这并不意味着我们应该这样称呼它)

暂无
暂无

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

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