cant figure out how to calculate averages
using System;
namespace MyApp
{
internal class Program
{
static void Main(string[] args)
{
Intro();
double test1 = score1("Please enter the score of the first test");
while (test1 > -999)
{
test1 = score1("Please enter the score of your test");
}
double total = grade(test1);
letter(total);
Ending();
}
public static void Intro()
{
Console.Title = ("EX38");
Console.WindowHeight = 15;
Console.WindowWidth = 60;
Console.ForegroundColor = ConsoleColor.Green;
}
public static double score1(string s)
{
Console.WriteLine(s);
double test1 = Convert.ToDouble(Console.ReadLine());
return test1;
}
public static double grade(double test1)
{
double total = (test1+999) / 5;
Console.WriteLine("The average of the grades is {0:f1}", total);
return total;
}
public static void letter(double total)
{
if (total > 89)
{
Console.WriteLine("This is an A");
}
if (total < 89 && total > 79)
{
Console.WriteLine("This is an B");
}
if (total < 79 && total > 69)
{
Console.WriteLine("This is an C");
}
if (total < 69 && total > 59)
{
Console.WriteLine("This is an D");
}
if (total < 59)
{
Console.WriteLine("This is an F");
}
}
public static void Ending()
{
Console.SetCursorPosition(Console.WindowWidth / 2 - 15, Console.WindowHeight - 2);
Console.WriteLine("Press enter twice to close window.");
Console.ReadLine();
}
}
}
26 Replies
I am trying to make a console application that allows the user to enter in multiple test scores and if -999 is entered it will show show the average of the scores
And which part doesn't work?
its more about me not knowing what to do because currently it will just display -198 This is a F because it doesnt calculate any numbers entered prior to the -999
You're never tracking the amount of items entered
So you cannot calculate an average
I dont know how i would do that
You can have a variable that tracks it
That increments with each loop
so like this?
Intro();
double test1 = score1("Please enter the score of the first test");
int count = 0;
while (test1 > -999)
{
test1 = score1("Please enter the score of your test");
count++;
}
double total = grade(test1);
letter(total);
Ending();
Yes, that's how you would track the amount of numbers entered
You'd also have to use it later, though
Also, you're not accumulating the numbers entered anywhere, it seems
Your
score()
method returns a number
You assign it to a variable
But it's always the same variable
That you keep overwritingso how would i fix it?
You could use a list of numbers
Or you could keep adding to some variable
Then you would just divide that sum by the amount of numbers entered
And you get your average
alright ill try to impement it
would this work?
You never change
testtotal
inside of the loop, and instead create a testtotal2
variable that exists only inside of the loop
So no, probably noti tested it out and it still doesnt work
Well, what does your
grade()
method do?public static double grade(double testtotal2, int count)
{
double total = (testtotal2 + 999) / count;
Console.WriteLine("The average of the grades is {0:f1}", total);
return total;
}
Why are you adding
999
to your total?to cancel out the -999 because to stop the loop i use -999
?
When you enter
-999
the loop stops
It doesn't add -999
to the totali think it does
Ah
Well
You're adding
testtotal + test1
to testtotal2
for some reason
Since testtotal
never changes, you keep overwriting the value in testtotal2
instead of adding to it
You need to be adding to the same variable
Incrementing it by consecutive valueslike this???
Almost
You're not adding the result of the first test to it
No idea why your first test is some special case that's outside of the loop tbh
But hey, it's a choice you made
im just lost rn
I can see that, yes
You have a lot of code for what is just two things:
1. Sum of numbers
2. Amount of numbers
In short, you want this:
Thank you so much