Using a double return string

In an application I am writing, one of the methods allows you to enter numbers entered by the user into letters.

For example, the user will enter the scores (like doubles) and the program will decide (when the criteria are met) to return the letter associated with the number. I originally wrote it 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 must be written with RETURNS in mind. So I think it should be a public string GetGrade(double scores)

And since in the array I will need:

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

      

Also, I would change everything console.writeline

to come back. However, when I do this, I get a syntax error telling me:

A local variable named score cannot be declared in this scope because it will give a different "score" value that is already being used in the parent or current scope to indicate something else.

So I understand that I cannot use score

because the header already contains score

. How do I get this to work the way I want?

0


source to share


3 answers


Your question is confusing.

And since its in an array ...

but none of your examples contain an array. Your method will 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!";
}

      



It seems to me that your method is not the problem, its code where you call it is the problem. You will have to post this to get the correct answer to your question.

If you are getting an array of "ratings" and converting them to a bunch of letter grades, you will need to have an array to store the letter ratings. Perhaps something like 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;
}

      

+3


source


Try changing the variable name in the for loop.

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

      



I'm guessing it scores

is an array of doubles. Is it correct?

If you return from an array, you only get the score for the first score ... You should probably return a correlated collection of scores.

+1


source


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

      

0


source







All Articles