public static string RatingCalculator(int input)
{
if (input < 10)
{
return string.Empty;
}
if (input > 10 && input < 20)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 21 && input < 40)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 41 && input < 70)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
if (input > 11 && input < 120)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
else
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" />";
}
}
答案 0 :(得分:11)
查看常用部分并尝试提取它们。
“全明星”的图像标记永远不会改变 您的“空星”图像标记永远不会更改 因此,您可以将这两者提取为可读性。
格式化程序字符串也是如此,总会有“5个连续的星型”
怎么样
string fs = @"<img src=\"/images/star.png\" alt=\"*\" />"; //Full Star
string es = @"<img src=\"/images/star_empty.png\" alt=\"-\" />"; //Empty Star
string format = @"{0}{1}{2}{3}{4}";
if(input < 10)
return string.Empty;
else if(input < 20)
return string.Format(format, fs, es, es, es, es);
else if(input < 40)
return string.Format(format, fs, fs, es, es, es);
else if(input < 70)
return string.Format(format, fs, fs, fs, es, es);
else if(input < 120)
return string.Format(format, fs, fs, fs, fs, es);
else
return string.Format(format, fs, fs, fs, fs, fs);
或者,您可以使用字符串构建器
string fs = @"<img src=\"/images/star.png\" alt=\"*\" />"; //Full Star
string es = @"<img src=\"/images/star_empty.png\" alt=\"-\" />"; //Empty Star
StringBuilder sb = new StringBuilder(fs);
//No need for `sb.Append (input > 10 ? fs : es);` as we'll test "input < 10" in the return statement.
sb.Append (input > 20 ? fs : es);
sb.Append (input > 40 ? fs : es);
sb.Append (input > 70 ? fs : es);
sb.Append (input > 120 ? fs : es);
return (input < 10) ? string.Empty : sb.ToString();
答案 1 :(得分:3)
代码中的许多字符串文字必然看起来非常丑陋。此外,我不确定为什么值中的差距是不恒定的(或者甚至不经常增加),但这不是一个大问题。
试试这个:
public static string RatingCalculator(int input)
{
int numStars;
if (input < 10)
return string.Empty;
else if (input < 20)
numStars = 1;
else if (input < 40)
numStars = 2;
else if (input < 70)
numStars = 3;
else if (input < 120)
numStars = 4;
else
numStars = 5;
var sb = new StringBuilder();
for (int i = 0; i < numStars; i++)
sb.Append("<img src=\"/images/star.png\" alt=\"*\" />");
for (int i = numStars; i < 5; i++)
sb.Append("<img src=\"/images/star_empty.png\" alt=\"-\" />");
return sb.ToString();
}
答案 2 :(得分:3)
很抱歉发布了一个JavaScript解决方案(在Mozilla Rhino上测试过),但我不太了解C#,我相信这里的算法很有意思。
var rating = function(input) {
var star = '<img src="/images/star.png" alt="*" />',
empty = '<img src="/images/star_empty.png" alt="*" />',
steps = [10, 20, 40, 70, 120],
max = steps.length;
for (var i=0; i<max; i++) {
if (input > steps[i]) {
print(star);
} else {
print(empty);
}
}
};
也许它有所帮助。
答案 3 :(得分:1)
public static string RatingCalculator(int input)
{
if (input < 10)
{
return string.Empty;
}
else if (input < 20)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
else if (input < 40)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
else if (input < 70)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
else if (input < 120)
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star_empty.png\" alt=\"-\" />";
}
else
{
return "<img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" /><img src=\"/images/star.png\" alt=\"*\" />";
}
}
确实没那么短,但可读性要好得多。
答案 4 :(得分:0)
public static string RatingCalculator(int input)
{
int nStars = 0;
if (input < 10)
return string.Empty;
else if (input < 20)
nStars = 1;
else if (input < 40)
nStars = 2;
else if (input < 70)
nStars = 3;
else if (input < 120)
nStars = 4;
else
nStars = 5;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < nStars; i++)
sb.Append("<img src=\"/images/star.png\" alt=\"*\" />");
for(int i = nStars; i < 5; i++)
sb.Append("<img src=\"/images/star_empty.png\" alt=\"-\" />");
return sb.ToString();
}
答案 5 :(得分:0)
我正在尝试一个循环(注意逻辑关闭,因为我放弃了解决方案):
public static string RatingCalculator(int input)
{ 字符串评级=“”;
for(i=0;i<120;i+=10)
{
if(input > i)
rating += "<img src=\"/images/star.png\" alt=\"*\" />";
else
rating += "<img src=\"/images/star_empty.png\" alt=\"-\" />";
}
return rating;
}
然后注意到收视率10,20,40,70等的非线性增长...
使用上面的答案,您可以通过将字符串分解为:
来缩短它并使其更具可读性String star = "<img src=\"/images/star.png\" alt=\"*\" />";
String emptyStar = "<img src=\"/images/star_empty.png\" alt=\"-\" />";
上面的答案增加了它:
if (input < 10)
{
return string.Empty;
}
else if (input < 20)
{
return star+emptyStar+emptyStar+emptyStar+emptyStar;
}
...
答案 6 :(得分:0)
我会做这样的事情:
const string Star = "<img src=\"/images/star.png\" alt=\"*\" />";
const string Star_Empty = "<img src=\"/images/star_empty.png\" alt=\"-\" />";
public static string RatingCalculator(int input)
{
if (input < 10)
{
return string.Empty;
}
else if (input > 10 && input < 20)
{
return string.concat(Star, Star_Empty, Star_Empty, Star_Empty, Star_Empty);
}
else if (input > 21 && input < 40)
{
return string.concat(Star, Star, Star_Empty, Star_Empty, Star_Empty);
}
else if (input > 41 && input < 70)
{
return string.concat(Star, Star, Star, Star_Empty, Star_Empty);
}
else if (input > 11 && input < 120)
{
return string.concat(Star, Star, Star, Star, Star_Empty);
}
else
{
return string.concat(Star, Star, Star, Star, Star);
}
}
答案 7 :(得分:0)
int starCount;
if (input < 10)
return string.Empty;
else if (input < 20)
starCount = 1;
else if (input < 40)
starCount = 2
else if (input < 70)
starCount = 3;
else if (input < 120)
starCount = 4;
else
starCount = 5;
StringBuilder build = new StringBuilder();
int i;
for(i = 0; i < starCount; i++)
build.Append("<img src=\"/images/star.png\" alt=\"*\" />");
for(; i < 5; i++)
build.Append("<img src=\"/images/star_empty.png\" alt=\"*\" />");
return build.ToString();
仍然不是很好,但是如果你合理化你的输入(所以它不会跳过任意数量),你可以从常规整数除法得到starCount。
答案 8 :(得分:0)
public static string RatingCalculator(int input)
{
if (input > 120)
{
return GetStart(5);
}
if (input > 70)
{
return GetStars(4);
}
if (input > 40)
{
return GetStars(3);
}
if (input > 20)
{
return GetStars(2);
}
if (input > 10)
{
return GetStars(1);
}
else
{
return string.Empty;
}
}
//populate dictionary with values in your class ctor
private static IDictionary<int, string> images=new Dictionary<int,string>();
private static string GetStars(int stars)
{
if(images.ContainsKey(stars))
return images[stars];
return string.Empty;
}
答案 9 :(得分:0)
if (input < 10) return string.Empty;
int noOfStars = input >= 120 ? 5 : Math.Min(input / 10, 4);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++)
{
sb.Append(
string.Format("<img src=\"/images/{0}\" alt=\"*\" />",
i < noOfStars ? "star" : "starEmpty")
);
}
return sb.ToString();