在 C# 中重载方法
Overloading a method in C#
我正在做一项作业,但我一直在重载我的方法以重复 nth
次 main
中指定的次数。我的 class 中的最后一个方法称为 Collatz
,它包含在 do while
循环中,但它似乎没有正常工作,因为它应该 运行 循环 x 数量重载的 main 方法中指定的次数。这是我唯一想不通的部分。这是我的 Main
:
static void Main(string[] args)
{
Collatz thisCollatz = new Collatz(15);
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
thisCollatz.Iterate();
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
//// Overload of iterate method
thisCollatz.Iterate(10);
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
Console.WriteLine("\nPAK..." + "\nProduced By: Jeremy Garcia");
Console.ReadKey();
}
这是我的 class:
class Collatz
{
// _Iteration is how many iterations of Collatz have run since initialization
// _CurrentVal is the current value of the Collatz process
private int _Iteration;
private int _CurrentVal;
public Collatz(int InitialValue)
// initializer
{
CurrentVal = InitialValue;
Iteration = 0;
}
public int CurrentVal
// returns the current Collatz value or -- within the class -- allows it to be set
{
get { return _CurrentVal; }
private set
{
if (value > 0)
{
_CurrentVal = value;
}
else
{
throw new Exception("CurrentVal is not a positive integer");
}
}
}
public int Iteration
// returns the current number of Collatz iterations or
// -- within the class -- allows it to be set
{
get { return _Iteration; }
private set { _Iteration = value; }
}
public void Iterate()
// Executes one iteration of Collatz creating a
// new CurrentVal from the existing CurrentVal
{
if (_CurrentVal != 1)
{
if (_CurrentVal % 2 == 0)
{
_CurrentVal = _CurrentVal / 2;
_Iteration ++;
}
else if (_CurrentVal % 2 != 0)
{
_CurrentVal = (_CurrentVal * 3) + 1;
_Iteration ++;
}
else
{
_CurrentVal = 1;
_Iteration = 1;
}
}
}
public void Iterate(int iterations)
// Check if CurrentVal is (already) 1 -- don't calculate
// new CurrentVal nor increment Iteration if so
// Otherwise calculate new CurrentVal and increment Iteration
{
do
{
if (_CurrentVal != 1)
{
if (_CurrentVal % 2 == 0)
{
_CurrentVal = _CurrentVal / 2;
_Iteration++;
}
else if (_CurrentVal % 2 != 0)
{
_CurrentVal = (_CurrentVal * 3) + 1;
_Iteration++;
}
else
{
_CurrentVal = 1;
_Iteration = 1;
}
}
} while (iterations != 0);
}
}
提前感谢您的帮助。我不想重写整个程序,只是让最后一个方法起作用。再次感谢。
您需要减少迭代计数器以避免无限循环。类似于:
do
{
...
iterations--;
} while (iterations != 0);
完成工作后,您需要确保将 iterations
变量设置为 0 以退出 do/while 循环。
我正在做一项作业,但我一直在重载我的方法以重复 nth
次 main
中指定的次数。我的 class 中的最后一个方法称为 Collatz
,它包含在 do while
循环中,但它似乎没有正常工作,因为它应该 运行 循环 x 数量重载的 main 方法中指定的次数。这是我唯一想不通的部分。这是我的 Main
:
static void Main(string[] args)
{
Collatz thisCollatz = new Collatz(15);
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
thisCollatz.Iterate();
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
//// Overload of iterate method
thisCollatz.Iterate(10);
Console.WriteLine(
string.Format("Collatz iteration is {0} and value is {1}",
thisCollatz.Iteration, thisCollatz.CurrentVal));
Console.WriteLine("\nPAK..." + "\nProduced By: Jeremy Garcia");
Console.ReadKey();
}
这是我的 class:
class Collatz
{
// _Iteration is how many iterations of Collatz have run since initialization
// _CurrentVal is the current value of the Collatz process
private int _Iteration;
private int _CurrentVal;
public Collatz(int InitialValue)
// initializer
{
CurrentVal = InitialValue;
Iteration = 0;
}
public int CurrentVal
// returns the current Collatz value or -- within the class -- allows it to be set
{
get { return _CurrentVal; }
private set
{
if (value > 0)
{
_CurrentVal = value;
}
else
{
throw new Exception("CurrentVal is not a positive integer");
}
}
}
public int Iteration
// returns the current number of Collatz iterations or
// -- within the class -- allows it to be set
{
get { return _Iteration; }
private set { _Iteration = value; }
}
public void Iterate()
// Executes one iteration of Collatz creating a
// new CurrentVal from the existing CurrentVal
{
if (_CurrentVal != 1)
{
if (_CurrentVal % 2 == 0)
{
_CurrentVal = _CurrentVal / 2;
_Iteration ++;
}
else if (_CurrentVal % 2 != 0)
{
_CurrentVal = (_CurrentVal * 3) + 1;
_Iteration ++;
}
else
{
_CurrentVal = 1;
_Iteration = 1;
}
}
}
public void Iterate(int iterations)
// Check if CurrentVal is (already) 1 -- don't calculate
// new CurrentVal nor increment Iteration if so
// Otherwise calculate new CurrentVal and increment Iteration
{
do
{
if (_CurrentVal != 1)
{
if (_CurrentVal % 2 == 0)
{
_CurrentVal = _CurrentVal / 2;
_Iteration++;
}
else if (_CurrentVal % 2 != 0)
{
_CurrentVal = (_CurrentVal * 3) + 1;
_Iteration++;
}
else
{
_CurrentVal = 1;
_Iteration = 1;
}
}
} while (iterations != 0);
}
}
提前感谢您的帮助。我不想重写整个程序,只是让最后一个方法起作用。再次感谢。
您需要减少迭代计数器以避免无限循环。类似于:
do
{
...
iterations--;
} while (iterations != 0);
完成工作后,您需要确保将 iterations
变量设置为 0 以退出 do/while 循环。