I am having trouble returning a list from a method called "omvandlaBetyg" which translates to "convert grades". I have added a total of 5 'char' to my list. I wish to return the list from the method.
With many attempts I have not been able to solve this issue. I am sorry for having set the names of the lists/variables to another language.
If it is too difficult to try and find a solution for my issue then I will translate it as soon as possible.
Thanks in advance.
static void Main(string[] args)
{
string[] ämnen = { "Matematik", "Svenska", "Engelska", "Historia", "Fysik" };
int[] poäng = new int[5];
List<char> betyg = new List<char>(); // This is the list that is currently empty
Program.LäsPoäng(poäng);
Program.omvandlaBetyg(poäng, betyg);
Program.skrivUtBetyg(betyg, ämnen);
}
static int LäsPoäng(int[] poäng)
{
Console.Write("Ange totalpoäng för matematik:");
int matPoäng = Convert.ToInt32(Console.ReadLine());
Console.Write("Ange totalpoäng för svenska:");
int svePoäng = Convert.ToInt32(Console.ReadLine());
Console.Write("Ange totalpoäng för engelska:");
int engPoäng = Convert.ToInt32(Console.ReadLine());
Console.Write("Ange totalpoäng för historia:");
int hisPoäng = Convert.ToInt32(Console.ReadLine());
Console.Write("Ange totalpoäng för fysik:");
int fysPoäng = Convert.ToInt32(Console.ReadLine());
poäng[0] = matPoäng;
poäng[1] = svePoäng;
poäng[2] = engPoäng;
poäng[3] = hisPoäng;
poäng[4] = fysPoäng;
return poäng[4];
}
static char omvandlaBetyg(int[] poäng, List <char> betyg)
{
foreach (int i in poäng)
{
if (i < 50)
{
betyg.Add('F');
}
else if (i >= 50 && i < 60)
{
betyg.Add('E');
}
else if (i > 60 && i < 75)
{
betyg.Add('D');
}
else if (i > 75 && i < 90)
{
betyg.Add('C');
}
else if (i > 90 && i < 100)
{
betyg.Add('B');
}
else if (i == 100)
{
betyg.Add('A');
}
}
return betyg; // This is the list that has been updated with newly added char's
// that i wish to return from this method
}
static void skrivUtBetyg(List<char> betyg, string[] ämnen)
{
foreach (char item in betyg)
{
Console.WriteLine("Betyg i : " item );
}
}
static void Statistik()
{
}
}
}
CodePudding user response:
You can just change the return type of the method omvandlaBetyg from char to List<char>
Here I fixed your code:
static List<char> omvandlaBetyg(int[] poäng, List <char> betyg)
{
foreach (int i in poäng)
{
if (i < 50)
{
betyg.Add('F');
}
else if (i >= 50 && i < 60)
{
betyg.Add('E');
}
else if (i > 60 && i < 75)
{
betyg.Add('D');
}
else if (i > 75 && i < 90)
{
betyg.Add('C');
}
else if (i > 90 && i < 100)
{
betyg.Add('B');
}
else if (i == 100)
{
betyg.Add('A');
}
}
return betyg;
}
CodePudding user response:
I'm assuming you're trying to learn C#, but there are a few odd things in your code; here's what it could look like:
static void Main(string[] args)
{
var ämnen = new string[] { "Matematik", "Svenska", "Engelska", "Historia", "Fysik" };
// No need to create your lists/arrays here and pass them to these methods
// as they can return them directly
var poäng = LäsPoäng(ämnen);
var betyg = OmvandlaBetyg(poäng);
SkrivUtBetyg(betyg);
}
// The purpose of that method is to query grades and return them
static List<int> LäsPoäng(string[] ämnen)
{
// Create your list here, and return it when you've asked all grades
var poäng = new List<int>();
// Redundant code, you've got a list of subjects so you might as well use them
// This means you could add new subjects without changing this method
foreach (var str in ämnen)
{
Console.Write($"Ange totalpoäng för {str.ToLower()}:");
poäng.Add(Convert.ToInt32(Console.ReadLine()));
}
return poäng;
}
// The purpose of that method is to convert integer grades to A-F
// Method name should use camel case
static List<char> OmvandlaBetyg(List<int> poäng)
{
// Create your list here, and return it when you've converted all grades
var betyg = new List<char>();
foreach (int i in poäng)
{
if (i < 50)
betyg.Add('F');
else if (i < 60) // No need to check lower bound, previous "if" takes care of it
betyg.Add('E');
else if (i < 75)
betyg.Add('D');
else if (i < 90)
betyg.Add('C');
else if (i < 100)
betyg.Add('B');
else
betyg.Add('A');
}
return betyg;
}
// Camel case
static void SkrivUtBetyg(List<char> betyg)
{
foreach (char item in betyg)
{
Console.WriteLine($"Betyg i : {item}");
}
}
It could still be improved and is by no means perfect, but hopefully it will give you some pointers.
