创建一个针对表进行验证的方法

时间:2014-12-18 00:43:16

标签: c# winforms

基本上我正在尝试创建一个方法,该方法将采用两个参数并对表运行以确定结果并返回它...表格在下面

enter image description here

例如,如果年龄为16且分数为50,则该方法应将评分返回为GOOD

我正在考虑的代码结构是这样的......

public void test(int age, int score)
        {
            while (age>=15 && age<=19 )
            {
              //code..
               break;

            }
        }

但是不能正确......任何想法?

3 个答案:

答案 0 :(得分:1)

要检查值是否属于给定范围,您需要&&(而不是||)条件:

 lowerBound <= age && age <= upperBound

对于其余代码,使用每个范围的if条件的硬编码序列(即,如果性能非常重要,并且特定范围的概率在范围之间显着不同)或者只是迭代年龄范围的集合/结果范围(对于这种情况,考虑将数据表示为{年龄范围,列表或结果范围的列表})

答案 1 :(得分:1)

if (Enumerable.Range(15,19).Contains(age))
{
   if (Enumerable.Range(0,30).Contains(score))
   {
   ...
   }
   else
   if (Enumerable.Range(31,38).Contains(score))
   {
   ...
   }
   else
   ...
}
else
if (Enumerable.Range(20,29).Contains(age))
{
....
}
else
if (Enumerable.Range(30,39).Contains(age))
{
....
}
else
....

答案 2 :(得分:1)

我会试着看看这是否能正常运作

online demo

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    struct AgeScore
    {
        public MinMax Age;
        public MinMax Score;

        public AgeScore(MinMax Age, MinMax Score)
        {
            this.Age = Age;
            this.Score = Score;
        }

        public override string ToString()
        {
            return string.Format("Age {0} - Score {1}", Age, Score);
        }
    }
    struct MinMax
    {
        int min;
        int max;
        public bool Between(int num)
        {
            return num >= min && num <= max;
        }

        public MinMax(int min, int max)
        {
            this.min = min;
            this.max = max;
        }

        public override string ToString()
        {
            return string.Format("Range: {0}-{1}", min, max);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var lookup = new Dictionary<string, AgeScore>();

            BuildRating(lookup);

            process(lookup, 16, 50);
            process(lookup, 16, 70);
            process(lookup, 45, 20);
            process(lookup, 10, 50);

            Console.Read();
        }

        static void BuildRating(Dictionary<string, AgeScore> rating)
        {
            //ONLY DOING A FEW FOR DEMO PURPOSE

            //age 15-19 score 60+
            rating.Add("EXCELLENT", new AgeScore(new MinMax(15, 19), new MinMax(60, int.MaxValue)));

            //age 15-19 score 45-59
            rating.Add("GOOD", new AgeScore(new MinMax(15, 19), new MinMax(45, 59)));

            //age 40-49 score <25
            rating.Add("POOR", new AgeScore(new MinMax(40, 49), new MinMax(int.MinValue, 24)));
        }

        static void process(Dictionary<string, AgeScore> rate, int age, int score)
        {
            var rating = rate.Where(x => x.Value.Age.Between(age) &&
                                         x.Value.Score.Between(score)).FirstOrDefault();

            Console.WriteLine("For age {0} and score {1} you have this rating is {2}\n", age, score, string.IsNullOrEmpty(rating.Key) ? "Unrated" : rating.ToString());
        }
    }
}