我正在寻找一种基于 C# 或 Java 中的各种分数返回字符串的好方法。例如,假设我有以下用双打表示的分数:
double scoreIQ = 4.0;
double scoreStrength 2.0;
double scorePatience 9.0;
假设我想根据上述分数返回一个字符串。什么是一种以清晰、易于理解的方式来构建它的好方法,与以下不同:
public static string ReturnScore(double scoreIQ, double scoreStrength, double scorePatience)
{
if (scoreIQ <= 5.0)
{
if (scoreStrength <= 5.0)
{
if (scorePatience <= 5.0)
{
return "You're stupid, weak, and have no patience";
}
else if (scorePatience <= 7.5)
{
return "You're stupid, weak, and have some patience";
}
else
{
return "You're stupid, weak, and have a lot of patience";
}
}
//Continue elseif return string permutations here
}
//Continue elseif return string permutations here
}
立即想到的事情是删除硬编码的字符串并使用关键字/短语的字符串创建器,根据分数创建句子。我不想使用嵌套的 if 语句,因为如果我想超过 3 分怎么办?排列将呈指数增长。
以上只是一个例子,我正在寻找一些可以处理这种情况的结构。显然,这是错误的做法。我正在寻找能引导我朝正确方向前进的人。谢谢!