views:

372

answers:

3

In the application I'm writing, one of the methods allows for the numbers the user entered to be turned into letters. For example, the user will be entering grades (as doubles) and the program will decide (when the criteria is met) to return the letter associated with the number. Initially, I had it written like this:

public void GetGrade(double scores) Console.Write("Score of {0} earns: ", score);

    if (score >= 95.0)
        Console.WriteLine("A+");
    else if (score >= 90.0)
        Console.WriteLine("A");
    else if (score >= 85.0)
        Console.WriteLine("B+");
    else if (score >= 80.0)
        Console.WriteLine("B");
    else if (score >= 75.0)
        Console.WriteLine("C+");
    else if (score >= 70.0)
        Console.WriteLine("C");
    else if (score >= 65.0)
        Console.WriteLine("D+");
    else if (score >= 60.0)
        Console.WriteLine("D");
    else
        Console.WriteLine("F");

But it needs to be written with a RETURN in mind. So, I think it should be public string GetGrade(double scores) And since it's in an array I would need:

foreach(double score in scoress) { THE CODE I POSTED ABOVE }

Except I'd change all the console.writeline's to return. However, when I do that I get a syntax error telling me: "A local variable named score cannot be declared in this scope because it would give a different meaning to 'score', which is already used in parent or current scope to denote something else."

So, I gather that I cannot use score because the header already contains score. How do I go about getting this to work how I want it?

+3  A: 

Your question is confusing.

And since its in an array...

but none of your examples include an array. Your method would work fine as

public string ToGrade(double score)
{
  if (score >= 95.0)
        return "A+";
    else if (score >= 90.0)
        return "A";
  /* snip */
    else
        return "YOU GET NOTHING!  YOU LOSE!  GOOD DAY SIR!";
}

Seems to me your method isn't the issue, its the code in which you're calling it that's the problem. You'll have to post that to get a correct answer to your question.

If you're getting an array of "grades" and transforming them into a bunch of letter grades, you'll have to have an array to store the letter grades. Something like this may be what you need:

public static string[] ToGrade(double[] grades)
{
  // sanity checks go here
  string[] result = new string[grades.Length];
  for(int i = 0; i < grades.Length; i++)
    result[i] = ToGrade(grades[i]);
  return result;
}
Will
A: 

try changing the variable name in your for loop.

Foreach (double s in scores){...}

I presume scores is an array of doubles??

One question, if you return from an array you will only get the grade for the first score.. Maybe you should return a correlating collection of grades?

A: 
var list = [
    [95.0, "A+"],
    [90.0, "A"],
    [85.0, "B+"],
    [80.0, "B"],
    [75.0, "C+"],
    [70.0, "C"],
    [65.0, "D+"],
    [60.0, "D"]
];

 for (var i in list)
    if (score >= list[0])
     return list[1];
 return "F";
Din
Its in c#, which is why you're gonna get some -1's (not from me, tho)
Will
I wish people would post other language to questions labeled 'homework' since I use that tag for learning examples. +1
discorax