2017-04-15 31 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."); 
     } 

    }  
} 
相关问题