Reputation: 25
I'm starting with the C# language making console "apps". This one is to make grade averages. It doesn't show each name on each line with its own average that I still have to code. It works 100% until the //notation I've put on the code. Under that I think it's the main problem, something that I might have forgotten to add or something.
namespace Repeat
{
class Program
{
static void Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("grade averages\n");
Console.ResetColor();
Console.WriteLine("");
float studentNumber;
float testNumber;
string name = "0";
string test;
float average = 0;
Console.WriteLine("Type the student number");
float.TryParse(Console.ReadLine(), out studentNumber);
Console.WriteLine("Type the number of tests");
float.TryParse(Console.ReadLine(), out testNumber);
Console.WriteLine("");
for (int a = 1; a <= studentNumber; a++)
{
Console.WriteLine("Type the student name " + a + ":");
nome = Console.ReadLine();
for (int p = 1; p <= testNumber; p++)
{
Console.WriteLine("Type the grade of test " + p + ":");
prova = Console.ReadLine();
}
Console.WriteLine("");
}
//All above is working 100%
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Students average:");
Console.ResetColor();
Console.WriteLine("");
for (int a = 1; a <= studentNumber; a++)
{
Console.WriteLine(name + ":" + average);
}
Console.ReadKey();
}
}
}
Upvotes: 0
Views: 143
Reputation: 29252
You're entering the names of the students and the test scores, but you need to save those values somewhere so that you can display them again.
This is just a basic example. You could add another class to your project like this:
public class StudentScoreCollection
{
private List<float>() _testScores = new List<float>();
public string StudentName {get;set;}
public List<float> TestScores {get{return _scores}}
}
Then, in your main program, declare a variable like
var scores = new List<StudentScoreCollection>();
Each time you start with a new student name (inside the first for
loop)
declare
var score = new StudentScore();
score.Name = Console.ReadLine();
Then as each score is entered,
score.TestScores.Add(float.Parse(Console.ReadLine());
(There's no validation here - it will blow up if they enter something that isn't a number.)
Then at the end,
scores.ForEach(score => Console.WriteLine(score.name + score.TestScores.Average());
I wrote that as a LINQ expression. That can be confusing at first. It's more readable as a for-each
loop. This is the exact same thing:
foreach(var score in scores)
{
Console.WriteLine(score.name + score.TestScores.Average();
}
Once you create a class that represents data that logically belongs together - like a name and a list of related scores - the rest becomes a little easier to work with.
Upvotes: 1
Reputation: 252
Every time you read the names and test-results of the students, you overwrite the previous value. You should store the names and grades (and possibly do some averaging-calculations). One example would be to use a Dictionary<string,List<int>>
or Dictionary<string,List<string>>
, where the name is stored in the Key
, and the grades are stored as a list in the Value
.
The Dictionary class is from the namespace System.Collections.Generic
.
Here is the documentation for the Dictionary.
Upvotes: 1