0

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.writelines 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 the way I want it to?

3
  • Looks like an obvious name collision. Could you post the whole piece of code? Commented Nov 29, 2008 at 19:39
  • This one method of nine or so. That'd be quite a bit. I manged to use that same foreach statement in three other methods, but those didn't have the exact same header names like this one does. So I assume you're right in this assessment of name collision. I'm just now sure how to change it. Commented Nov 29, 2008 at 19:48
  • Try cutting out the code until (a) it's simple enough to post here or (b) the problem goes away. In the case of (b), the name collision is occurring in the deleted code. Commented Nov 29, 2008 at 20:14

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try changing the variable name in your for loop.

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

I assume scores is an array of doubles. Is this correct?

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.

Comments

0
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";

2 Comments

Its in c#, which is why you're gonna get some -1's (not from me, tho)
I wish people would post other language to questions labeled 'homework' since I use that tag for learning examples. +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.