A good way to do a string search based on multiple evaluations in C # or Java

I am looking for a good way to return a string based on various evaluations in C # or Java. For example, suppose I have the following scores represented by doubles:

double scoreIQ = 4.0; double scoreStrength 2.0; double scorePatience 9.0; 

Let's say I wanted to return a string based on the above estimates. What would be a good way to structure this in a clear, understandable way, as opposed to as follows:

 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 } 

What immediately comes to mind would be to remove the hard-coded strings and use a keyword / phrase string creator that creates suggestions based on ratings. I do not want to use nested if statements, because if I want to go beyond 3 points? Permutations will increase exponentially.

The above is just an example, and I'm looking for some structure that can handle such a scenario. This is clearly the wrong way to do this. I am looking for someone to lead me in the right direction. Thanks!

+4
source share
3 answers

If you have a series of yes no values, one way is bitmask , where each bit represents one of the yes no values.

For example, let bit 0 (a bit numbered from right to left, starting at 0) be true for scoreIQ> = 4.0 and 0 otherwise.

Let bit 1 be true for scoreStrength> = 2.0 and 0 otherwise.

Let bit 2 be true for scorePatience> = 9.0 and 0 otherwise.

So, if you want to find out if someone has a high IQ and is very patient, but he is not very strong, you can use the AND operator with bit mask 101 to perform this test.

Other examples:

 000 - person has poor IQ, is not strong, and is not patient 001 - person has good IQ, is not strong, and is not patient 011 - person has good IQ, is strong, and is not patient 110 - person has poor IQ, is strong, and is patient 

As you add new criteria, you simply use a different bit for the new criteria.

So, to create a bitmask to check the characteristics you care about, you can simply do something like this:

 // find out if person has good IQ and is patient, but not strong int mask = 0; mask |= (1<<0); // set bit 0 (good IQ) mask |= (1<<2); // set bit 2 (patient) if (personsMask & mask == mask) { // you know the person has a good IQ and is patient, but they are `not` strong } 
+2
source

In Java, you can use NavigableMap (no idea that there is something like that in C #)

 TreeMap<Double, String> patience = new TreeMap<Double, String>(); patience.put(0.0, "no"); patience.put(5.0, "some"); patience.put(7.5, "a lot of"); System.out.println("You have " + patience.floorEntry(6.0).getValue() + " patience!"); 

See http://docs.oracle.com/javase/7/docs/api/java/util/NavigableMap.html

+2
source

Using a dictionary, you can do something like this ...

 class Program { static void Main(string[] args) { ScoreTextTranslator dec = new ScoreTextTranslator(); dec.IfAtLeast(0, "stupid"); dec.IfAtLeast(5, "average intelligence"); dec.IfAtLeast(6, "smart"); Console.WriteLine(dec.GetTextForScore(5.7f)); } } class ScoreTextTranslator { Dictionary<float, string> backing = new Dictionary<float, string>(); public void IfAtLeast(float value, string text) { backing.Add(value, text); } public string GetTextForScore(float ActualScore) { var scoreRange = backing.Keys.Where(a => a <= ActualScore); if (scoreRange.Count() == 0) throw new Exception("No valid score text exists"); else return backing[scoreRange.Max()]; } } 
+1
source

All Articles