2013-02-19 90 views
-2

我知道有像vb到c#轉換器應用程序那裏的東西,但我所尋找的是有點不同。我需要一個轉換器來幫助我將這個「for」循環轉換爲「while」循環。這裏是我爲「Integer Factory」設計的代碼(你可以看到底部的「for」循環 - 這是需要轉換的東西)。我還有其他幾個循環,這就是爲什麼我需要一個應用程序(最好是wysiwyg)。謝謝!for循環轉換器

int IntegerBuilderFactory(string stringtobeconvertedbythefactory) 
{ 
     string strtmp = stringtobeconvertedbythefactory; 

     int customvariabletocontrolthethrottling; 

     if (strtmp.Length > 0) 
     { 
       customvariabletocontrolthethrottling = 1; 
     } 
     else 
     { 
       customvariabletocontrolthethrottling = 0; 
     } 

     for (int integersforconversiontostrings = 0; integersforconversiontostrings < customvariabletocontrolthethrottling; integersforconversiontostrings++) 
     { 
       return int.Parse(strtmp); 
     } 

     try 
     {    
       return 0; 
     } 
     catch (Exception ex) 
     { 
       // Add logging later, once the "try" is working correctly 

       return 0; 
     } 
} 
+6

你需要一個應用程序,重構「幾個for-loops」while循環?爲什麼你不能手動做? – 2013-02-19 13:32:04

+1

爲什麼即使是一個for循環,如果它返回它轉換的第一個元素?你有沒有試圖以某種方式簡化代碼,或者你有這個實際的代碼? – R0MANARMY 2013-02-19 13:36:11

+0

@ R0MANARMY,這是我過去幾天一直在努力的真實代碼。我正在簡化轉換流程。我使用循環來提高安全性和內存效率。我會開放給你可能有的任何建議! – 2013-02-19 13:50:28

回答

0
int integersforconversiontostrings = 0; 
while (integersforconversiontostrings < customvariabletocontrolthethrottling) 
{ 
    return int.Parse(strtmp); 
    integersforconversiontostrings++ 
} 
2

每一個for循環(for(initializer;condition;iterator)body;)本質上是

{ 
    initializer; 
    while(condition) 
    { 
     body; 
     iterator; 
    } 
} 

現在你可以利用這些知識來創建一個代碼轉換爲您所選擇的重構工具。

順便說一句,該代碼看起來可怕...

int IntegerBuilderFactory(string stringToParse) 
{ 
    int result; 
    if(!int.TryParse(stringToParse, out result)) 
    { 
     // insert logging here 
     return 0; 
    } 

    return result; 
} 

做起來難。

+0

那麼,我不能一定依賴這個邏輯的每個實例。我需要一個考慮所有因素的應用程序。 – 2013-02-19 13:40:24

+0

考慮哪些_considerations_?除了不可編譯的代碼外,我想不出任何... – Nuffin 2013-02-19 13:44:34

+0

需要注意的地方是使用'continue'語句。在for循環中,迭代語句將自動運行。這不是你的轉換'while'循環的情況。 – jerry 2013-02-19 15:33:13