2017-06-19 78 views
-1

只是在老問題上的一些背景...... Old version of the question
所以,經過一些測試和幾十個斷點......我的代碼被破壞了。 ,我已經四捨五入下來是這一功能的話:C# - 隨機字節布爾[] ...和返回不起作用?

 public BigInteger getNum() 
     { 
      BigInteger rtrnVal = 0; 
      int pow = 0; 
      foreach (bool b in _arr) 
      { 
       rtrnVal += (b ? BigInteger.Pow(2, pow) : 0); 
       pow++; 
      } 
      return rtrnVal; 
     } 

而這一次......

 public numToBin(object n) 
     { 
      assignType(n); //Check if the type is valid 
      BigInteger r = new BigInteger(Convert.ToInt32(n)); //Make sure that the type is valid 
      _arr = new List<bool>(); //Refresh the internal array of booleans 
      while (r != 0) //While we arent left with 0... 
      { 
       //if (!first) if (_arr.Last()) r += 1; //If the last one was true, then add one, because we want the ceiling (round up not down) 
       _arr.Add(!r.IsEven); //Add to the list 
       r = r/2; //Divide by two... I think this may potentially be 
      } 
     } 

但定了!感謝您指點我正確的方向!

*順便說一句,錯誤是當我扭轉列表。

+4

如果你知道你的問題在哪裏(第123行numToBin(object n)方法),那麼只需簡化你的問題,並通過只輸入失敗的代碼和更多關於預期結果的細節來更清楚。我在看你的方法,我不明白你在做什麼。 – Etienne

+0

好吧 在一會兒我會添加評論。 –

+1

而不是將所有方法調用嵌套在一行中,將它們拆分爲單獨的行可能會更容易,因此您可以檢查方法的返回值。 –

回答

-1
public BigInteger getNum() 
    { 
     BigInteger rtrnVal = 0; 
     int pow = 0; 
     foreach (bool b in _arr) 
     { 
      rtrnVal += (b ? BigInteger.Pow(2, pow) : 0); 
      pow++; 
     } 
     return rtrnVal; 
    } 

而這一次......

public numToBin(object n) 
    { 
     assignType(n); //Check if the type is valid 
     BigInteger r = new BigInteger(Convert.ToInt32(n)); //Make sure that the type is valid 
     _arr = new List<bool>(); //Refresh the internal array of booleans 
     while (r != 0) //While we arent left with 0... 
     { 
      //if (!first) if (_arr.Last()) r += 1; //If the last one was true, then add one, because we want the ceiling (round up not down) 
      _arr.Add(!r.IsEven); //Add to the list 
      r = r/2; //Divide by two... I think this may potentially be 
     } 
    } 

是代碼的正確版本。

+0

這裏似乎沒有答案。 –

+0

我應該將編輯移動到這裏嗎? –