2013-04-11 68 views
0

對不起,如果這可能不是以最好的方式解釋,但我基本上想要做的是顯示我創建的計算的輸出。這個計算是古埃及乘法(我給了一個故事來創建一個程序,用戶可以選擇使用這種方法計算值,並注意我們沒有大聲使用*和/運算符),我希望能夠顯示正在使用的權力,價值計算和整體結果。如果可能的話,我想把所有這些輸出都返回到彈出框中,但我不確定我將如何看待它,因爲我是C#(學徒)的新手。顯示計算過程的輸出

這裏是我多麼希望輸出

Powers: 1 + 4 + 8 = 13 
Values: (1 * 238) + (4 * 238) + (8 * 238) 
Result: 238 + 952 + 1904 = 3094 

以下是我在一分鐘爲古egyption乘法的代碼示例: 注iReturnP =電源,iReturnN =值,iReturn =結果

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace SimpleMath 
{ 
    public class AEM : IOperation 
    { 
     public int Calculate(int i, int j) 
     { 

      int[] ints = new int[] { i, j }; 
      Array.Sort(ints); 
      List<int> powers = new List<int>(); 
      int power = 1; 
      int first = ints[0]; 
      int iReturn = 0; 
      int iReturnP = 0; 
      int iReturnN = 0; 
      do 
      { 
       powers.Add(power); 
       power = new Multiply().Calculate(power, 2); 
      } while (power <= first); 
      iReturnP += first; 
      while (first > 0) 

      { 
       int next = powers.LastOrDefault(x => x <= first); 
       first -= next; 
       int powertotal = new Multiply().Calculate(next, i); 

       iReturnN += next; 
       iReturn += powertotal; 
      } 
      return iReturnP; 
      return iReturnN; 
      return iReturn; 

      } 
    } 
} 
+0

你不能返回的3倍!除了你真正的問題是什麼? – makc 2013-04-11 11:11:30

+0

這是他的問題... – 2013-04-11 11:12:28

+0

@makc初學程序員,削減一些懈怠 – LukeHennerley 2013-04-12 07:39:11

回答

0

一旦您運行return語句,該方法將退出。這意味着您的第二個和第三個return聲明將永遠不會發生。如果您確實想使用return聲明,我建議您返回包含所有3個值的int[]。還有很多其他方法可以解決這個問題。請注意,這隻會爲您提供總計。我會在你的路上讓你,但你必須自己做一些工作。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace SimpleMath 
{ 
public class AEM : IOperation 
{ 
    public static int[] Calculate(int i, int j) 
    { 

     int[] ints = new int[] { i, j }; 
     Array.Sort(ints); 
     List<int> powers = new List<int>(); 
     int power = 1; 
     int first = ints[0]; 
     int iReturn = 0; 
     int iReturnP = 0; 
     int iReturnN = 0; 
     do 
     { 
      powers.Add(power); 
      power = new Multiply().Calculate(power, 2); 
     } while (power <= first); 
     iReturnP += first; 
     while (first > 0) 

     { 
      int next = powers.LastOrDefault(x => x <= first); 
      first -= next; 
      int powertotal = new Multiply().Calculate(next, i); 

      iReturnN += next; 
      iReturn += powertotal; 
     } 
     return new int[]{iReturnP, iReturnN, iReturn}; 

     } 
} 
} 

然後在你的方法,你叫計算:

int[] results = AEM.Calculate(i, j); 
MessageBox.Show("Powers: " + results[0] + "\r\n Values: " + results[1] + "\r\n Results: " + results[2]); 
+0

感謝您的回答,這有助於我不會犯同樣錯誤嘗試返回多次。我們可以只顯示一個消息框然後顯示輸出嗎? – JayH 2013-04-11 11:19:29

+0

你可以用輸出做任何你想做的事情。告訴我你想達到什麼樣的目的以及你想要輸出到哪裏,我會盡力幫你 – 2013-04-11 11:23:38

+0

我知道,但是你想從計算方法裏面還是從你調用它的方法中顯示它? – 2013-04-11 11:26:35