我有一个变量来显示项目的RAG状态(红色,琥珀色,绿色)
var previousStatus = "R"
var currentStatus = "A"
我试图计算"趋势"有点喜欢
var trend = CalculateTrend(previous, current)
我正在努力寻找比
更优雅的解决方案 if (prev == current)
return "Stable";
if (prev == "R" && (current == "G" ||current == "A"))
return "Improving";
if (prev == "G" && (current == "R" ||current == "A"))
return "Declining";
if (prev == "A" && current == "G")
return "Improving";
if (prev == "A" && current == "R")
return "Declining";
关于"清洁剂的任何建议"溶液
答案 0 :(得分:5)
为每个状态创建一个带有整数值的enum
。
public enum Status
{
Red = 1,
Amber = 2,
Green = 3
}
然后使用int.CompareTo
方法。
switch(previous.CompareTo(current))
{
case -1:
return "Improving";
case 0:
return "Stable";
case 1:
return "Declining";
}
答案 1 :(得分:0)
使用数字系统。使用等效数值对值进行评级。然后你可以使用数学。
答案 2 :(得分:0)
如果您只想避免使用if语句,可以创建一个简单的查找字典:
private static string GetTrend(string prev, string cur)
{
var trends = new Dictionary<string, string[]>()
{
{"Stable", new[] {"AA", "RR", "GG"}},
{"Improving", new[] {"RG", "RA", "AG"}},
{"Declining", new[] {"GA", "GR", "AR"}},
};
return trends.Single(x => x.Value.Contains(prev + cur)).Key;
}