这就是程序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();
}
}
这里是构造函数类。Pascal三角法
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();
}
}
}
这里是我的StaticClass为长Pascal三角形
class StaticClass
{
public const int n = 15;
// I want to change the value of N using the Console Writeline.
}
n
需要更新,而不是const
使用static
;但是Console.WriteLine()
不是用来改变值的东西。你需要更清楚地知道你想做什么。您是否希望有人输入n
-的值与Console.ReadLine?
所以…从我收集到的——我认为你想改变你的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的值。https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants
请使用静态属性。
https://learn.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.
}
将公共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找一个更好的名字;类应该根据它们在更现实的世界中所代表的东西来命名(比如…(是的,它是一个静态类,但这并不意味着我们应该这样称呼它)