2017-04-15 61 views
-2

你好,其實我不知道怎麼稱呼這個問題。如何添加一些靜態值?

那麼,我有一個類對數組做一些關鍵測試。測試取決於陣列的長度。

class Test 
{ 
    public void CritTest(double[] n)  
    {  
     int j = n.Length(); 
     double zen;  

     if (j == 3)   
      zen = n.Max()/0.941; 

     if (j == 4)  
      zen = n.Max()/0.765; 

     if (j == 5)   
      zen = n.Max()/0.642; 

     if (j == 6)   
      zen = n.Max()/0.560; 

    //and so on... 
    }  
} 

喜歡這個具有相應的值取決於長度。如果到目前爲止如何處理它?

我有一個特定的關鍵表

+2

選自N的長度的恆定可計算,或者是它任意? –

+0

@IainBrown,以及它來自特殊關鍵表 – TomooMGL

+0

我應該使用任何數據表? – TomooMGL

回答

0

取決於您希望如何處理您的查找表(例如,你想從文件中加載了嗎?),但這裏的使用數組作爲查詢的簡單方式。範圍驗證留給讀者作爲練習。

class test 
{ 
    public void CritTest(double[] n)  
    {  
     double [] lookupTable = { 0,0,0, 0.941, 0.765, 0.642, 0.560}; 
     int j = n.Length(); 
     double zen;  

     zen = n.Max()/lookupTable[j]; 
    }  
} 
+1

你最好在'lookupTable'中不要有'0'... –

1
class Test 
{ 
    public Dictionary<int, double> GetTargetValues() 
    { 
     var result = Dictionary<int, double>(); 

     result.Add(3, 0.941); 
     result.Add(4, 0.765); 
     result.Add(5, 0.642); 
     result.Add(6, 0.560); 

     // You can fill this dictionary dynamically or add more lines statically 

     return result; 
    } 

    public void CritTest(double[] n)  
    {  
     int length = n.Length(); 
     double zen; 
     var targetValues = this.GetTargetValues(); 

     if (targetValues.ContainsKey(length)) 
     { 
      zen = n.Max()/targetValues[length]; 
     } 
     else 
     { 
      throw new Exception("The value ${length} is not registered."); 
     } 

    }  
} 
相關問題