So this is the Program cs. I want to change the value of int in my StaticClass.cs ** I want to delcare a printing sentence " How long do you want me to generate " then change the value of N in StaticClass.
class Program
{
static void Main(string[] args)
{
Constructor ct = new Constructor(StaticClass.n);
//ct.xmethod(StaticClass.n);
Console.ReadKey();
}
}
Here my constructor class. Method of Pascal Triangle
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();
}
}
}
Here my StaticClass for the long of Pascal Triangle
class StaticClass
{
public const int n = 15;
// I want to change the value of N using the Console Writeline.
}
CodePudding user response:
You cannot change the value of a const. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants
Use a static property instead.
static class StaticClass
{
public static int n = 15;
// I want to change the value of N using the Console Writeline.
}
CodePudding user response:
n
needs to be updatable then instead of const
use static
; but Console.WriteLine()
is not something you would use to change the value. You'll need to be more clear about what you want to do. Do you want someone to input the value of n
- with a Console.ReadLine?
So ... from what I gather -- I think you want to change your Main
to write your question: " How long do you want me to generate " then read the line for user input. Then print your triangle. Something like :
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();
}
Then from what I see ... your Constructor
needs to change:
public Constructor(int n)
{
triangle = new int[n][];
xmethod(n);
}
CodePudding user response:
Make your public const field a public static property instead:
public static int N { get; set; } = 15;
Change it like
StaticClass.N = 16;
You said "change it with WriteLine" - WriteLine outputs things, it doesn't change thing. Perhaps you meant ReadLine:
Console.WriteLine("How many levels? ");
string input = Console.ReadLine();
StaticClass.N = int.Parse(input);
Do please find a better name for StaticClass; classes should be named for what they represent in a more real world sense (like.. Configuration?) not what they are in a C# sense (yes, it's a static class, but it doesn't mean we should call it that)