2015-12-02 100 views
0

我最近開始「編碼」,我真的到了一個開始,這是我的第一個「項目」之一。它應該是SI轉換器,您可以在其中鍵入值,其單位和您希望轉換的單位。如果/其他條件在C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Program 
{ 
    class Program 
    { 
     static void Main() 
     { 
      decimal one = 1; 
      decimal two = 0.001m; 
      decimal three = 0.000001m; 
      decimal four = 0.000000001m; 
      decimal five = 0.000000000001m; 
      decimal answer; 


     begn: Console.WriteLine("SI converter!\nPlease, enter value: ");    
      decimal value = Convert.ToInt32(Console.ReadLine()); 

      Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter factor: "); 
      decimal factor = int.Parse(Console.ReadLine()); 

      if (factor == 1) 
      { 
       factor = one; 
      }else if (factor == 2) 
      { 
       factor = two; 
      }else if (factor == 3) 
      { 
       factor = three; 
      }else if (factor == 4) 
      { 
       factor = four; 
      }else if (factor == 5) 
      { 
       factor = five; 
      } 


      Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter the second factor: "); 
      decimal factor2 = Convert.ToInt32(Console.ReadLine()); 

      if (factor2 == 1) 
      { 
       factor2 = one; 
       answer = value * factor; 
       Console.WriteLine("The answer is : " + answer); 
      } 
      else if (factor2 == 2) 
      { 
       factor2 = two; 
      } 
      else if (factor2 == 3) 
      { 
       factor2 = three; 
      } 
      else if (factor2 == 4) 
      { 
       factor2 = four; 
      } 
      else if (factor2 == 5) 
      { 
       factor2 = five; 
      } 



       answer = value * factor/factor2;      
       Console.WriteLine("The answer is : " + answer); 


      Console.WriteLine("Go again?\nY/N");   
      char ans =char.Parse(Console.ReadLine()); 
      if (ans == 'y') 
      { 
       Console.Clear(); 
       goto begn; 

      } 
      if(ans=='n') 
      { 

       Console.ReadKey(); 
      } 


     } 


     } 
    } 

所以問題是,我真的不喜歡這個部分,我沒有任何想法如何做到這一點:

if (factor == 1) 
      { 
       factor = one; 
      }else if (factor == 2) 
      { 
       factor = two; 
      }else if (factor == 3) 
      { 
       factor = three; 
      }else if (factor == 4) 
      { 
       factor = four; 
      }else if (factor == 5) 
      { 
       factor = five; 
      } 

PS是的,我知道它的大概真的很糟糕,但我的第一try.And如果你能給我什麼建議,我會很開心:)

+0

我不明白這一點...有什麼錯的代碼? – bitwise

+6

給初學程序員的第一個建議:幫個忙,忘掉goto關鍵字;)。改用循環。 –

+0

更好的建議,忽略上述評論,並做你需要做的。樣式和技巧稍後提供 – bitwise

回答

1

使用開關狀態

switch (factor) 
     { 
      case 1: 
       factor = one; 
       break; 
      case 2: 
       factor = two; 
       break; 
      case 3: 
       factor = three; 
       break; 
      case 4: 
       factor = four; 
       break; 
       default: 
       //default when nothing happens in switch 
       factor = one; 
       break; 
     } 
+0

謝謝!我現在就試試看。 –

0

你可以做更多的「動態」創建一個數組與不同的值和他們適當的文本。然後用數組列的值打印相關文本。

string[] arrayText = { "one", "two", "three", "four", "five"}; 
//Remember than your array start at 0 
factor= factor - 1; 
System.console.WriteLine(arrayText[factor]); 

我認爲這就是你想要的!希望我會幫助你。

0

代替if/else塊的,你可以使用一個Dictionary<string, decimal>其中key(字符串)是您的預計投入和value(十進制)是輸入對應的變量。

當應用程序啓動時,您可以使用單獨的方法構建此字典,因爲這些值不會更改。使字典可用於程序的其餘部分(例如,使用public),並且可以使用相同的簡單檢查在任何需要的地方重新使用此信息。

一旦字典建成後,所有你所要做的就是檢查詞典包含輸入值和設定factor1(或factor2)相應:

string input; 
decimal factor1; 

Dictionary<string, decimal> factors = new Dictionary<string, decimal>(); 

factors.Add("1", one); 
factors.Add("2", two); 
factors.Add("3", three); 
factors.Add("4", four); 
factors.Add("5", five); 

input = Console.ReadLine(); 

if (factors.ContainsKey(input)) 
{ 
    factor1 = factors["input"]; 
} 

你也應該考慮沿某處添加else條件you screwed up, try again的行提示用戶重新輸入一個值。

此外,在您當前的代碼中,不要在輸入和計算中重複使用factor變量。只需輸入一個字符串並檢查特定的值。

如需更多幫助,我建議按照評論中的建議CodeReview。

1
using System; 

namespace Program 
{ 
    class Program 
    { 
     static decimal[,] factors = new decimal[4, 4] { 
      /*    To Milli  To Micro To Nano, To Pico */ 
      /* From Milli */ { 1m,   1000m,  1000000m, 1000000000m }, 
      /* From Micro */ { 0.001m,  1m,   1000m, 1000000m }, 
      /* From Nano */ { 0.000001m, 0.001m,  1m,  1000m  }, 
      /* From Pico */ { 0.000000001m, 0.000001m, 0.001m, 1m   } 
     }; 

     static void Main() 
     { 
      Console.WriteLine("SI converter!"); 

      while(true) 
      { 
       Console.Write("Please, enter value: "); 
       decimal value = Convert.ToInt32(Console.ReadLine()); 

       Console.Write("\n1) Milli(m)\n2) Micro(µ)\n3) Nano(n)\n4) Pico(p)\nFrom Units: "); 
       int fromUnits = int.Parse(Console.ReadLine()) - 1; 

       Console.Write("To Units: "); 
       int toUnits = int.Parse(Console.ReadLine()) - 1; 

       decimal factor = factors[fromUnits, toUnits]; 
       decimal answer = factor * value; 
       Console.WriteLine("The answer is : " + answer); 

       Console.Write("Go again? (Y/N): ");  
       string ans = Console.ReadLine(); 

       if(ans.ToUpper() == "N") 
        break; 
      } 
     } 
    } 
} 
+0

絕望地抵制用goto重寫我的例子的衝動...;) – bitwise

0

您可以使用類似下面的代碼:

decimal answer; 
decimal[] factorArray = new decimal[] { 1, 0.001m, 0.000001m, 0.000000001m, 0.000000000001m }; 

Console.WriteLine("SI converter!\nPlease, enter value: "); 
decimal value = Convert.ToInt32(Console.ReadLine()); 

Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter factor: "); 
decimal factor = int.Parse(Console.ReadLine()); 
if (factor >= 1 && factor <= factorArray.Length) 
{ 
    factor = factorArray[(int)factor - 1]; 
} 

Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter the second factor: "); 
decimal factor2 = Convert.ToInt32(Console.ReadLine()); 
if (factor2 >= 1 && factor2 <= factorArray.Length) 
{ 
    factor2 = factorArray[(int)factor2 - 1]; 
}