2017-01-30 4 views
0

私は代入に取り組んでいますが、それを繰り返すために私のメソッドオーバーロードに悩まされていますmainで指定された回数がnth倍です。私はというループの最後のメソッドをdo whileループでラップしていますが、オーバーロードされているメインメソッドで指定されているループ回数xを実行する必要があるため正しく動作していないようです。私が理解できないのはそれだけです。相続人は私のMainです:C#でメソッドをオーバーロードする

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 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); 
     } 
    } 

は、事前に協力いただきありがとうございます。私はプログラム全体を書き直すつもりはなく、最後に働く方法を得るだけです。再度、感謝します。

+1

「動作していないようです」は曖昧です。右下のポストのゴブス**関連** – Plutonix

+1

「機能していないように思われる」という意味に展開してください。あなたの画面は見えません。エラーが出ていますか?期待される行動は何ですか?それは実際に何をしていますか? **具体的にする** – Amy

+0

「do-while」は永遠に実行されていますか? - これは、そうであると思われるものです。ループは反復が0になることはないが、ループを維持するように設計されています。あなたのコードに何もあなたの反復をゼロに設定することはありません - したがって、無限ループです。ループ内のどこかで反復回数をカウントするか、ゼロに設定する必要があり、ループを終了させることができます。 – Darren

答えて

1

無限ループを避けるために、反復カウンタを減分する必要があります。ような何か:

do 
{ 
... 
iterations--; 
} while (iterations != 0); 
+0

パーフェクト!それは動作しますが、そのメインに10に設定されているときに私に11回の反復を示しています。何か不足していますか? –

+0

'thisCollat​​z.Iteration'の値が11回繰り返された場合、' thisCollat​​z.Iterate(); 'コールで既に1に設定されている可能性があります。 '_Iteration'の値がオブジェクト内で共有され、各操作が既存の値を変更することを思い出してください。 – yoogeeks

0

あなたが仕事をして終わったら、あなたはwhileループ/あなたのDOから抜け出すために0にごiterations変数を設定することを確認したいと思います。

関連する問題