C
C#2y ago
alexhoquan

❔ Trying to find a way so it shows the sum of all the numbers entered and the also the average.

Modifier votre code Q2 afin de
calculer/afficher la somme et la moyenne des nombres saisis
*/
//inscrire votre code ici
i = 1;

while (i < 5)
{
Console.WriteLine("Entrer un chiffre ");
ligne = Console.ReadLine();

boolok = int.TryParse(ligne, out NbrEntier);
if (boolok == true)
{
if (NbrEntier <= min)
{
min = NbrEntier;


}
i++;
}

}
Console.WriteLine("nombre min : " + min);
Console.ReadKey();
Modifier votre code Q2 afin de
calculer/afficher la somme et la moyenne des nombres saisis
*/
//inscrire votre code ici
i = 1;

while (i < 5)
{
Console.WriteLine("Entrer un chiffre ");
ligne = Console.ReadLine();

boolok = int.TryParse(ligne, out NbrEntier);
if (boolok == true)
{
if (NbrEntier <= min)
{
min = NbrEntier;


}
i++;
}

}
Console.WriteLine("nombre min : " + min);
Console.ReadKey();
36 Replies
mtreit
mtreit2y ago
Well, how do you think you might approach that?
alexhoquan
alexhoquanOP2y ago
by creating a static string that contains the sum of all numbers
mtreit
mtreit2y ago
Why a string? And why static? Wouldn't a numeric variable of some kind make more sense?
alexhoquan
alexhoquanOP2y ago
something like that?
No description
mtreit
mtreit2y ago
I think you just need a local variable. Not a static field. If you want to add numbers in C# you use a type like int
MODiX
MODiX2y ago
mtreit
REPL Result: Success
int a = 1;
int b = 5;
int sum = a + b;
Console.WriteLine(sum);
int a = 1;
int b = 5;
int sum = a + b;
Console.WriteLine(sum);
Console Output
6
6
Compile: 578.810ms | Execution: 69.356ms | React with ❌ to remove this embed.
alexhoquan
alexhoquanOP2y ago
hmm yea that i know but its the numbers i have entered that i want to add up together
mtreit
mtreit2y ago
What if you declare a variable sum outside of your loop?
alexhoquan
alexhoquanOP2y ago
like that?
No description
mtreit
mtreit2y ago
You should assign it an initial value.
int intsum = 0;
int intsum = 0;
alexhoquan
alexhoquanOP2y ago
int intsum=0;
mtreit
mtreit2y ago
Now you just need to write the code so that at the end that variable contains the sum of all the input values.
alexhoquan
alexhoquanOP2y ago
i think it has to do something with this
No description
No description
mtreit
mtreit2y ago
Uh...
MODiX
MODiX2y ago
mtreit
REPL Result: Success
int.MinValue
int.MinValue
Result: int
-2147483648
-2147483648
Compile: 354.892ms | Execution: 23.888ms | React with ❌ to remove this embed.
mtreit
mtreit2y ago
I don't know what you're trying to do there.
alexhoquan
alexhoquanOP2y ago
No description
alexhoquan
alexhoquanOP2y ago
basically all these numbers and make a sum of all the entered numbers
mtreit
mtreit2y ago
I understand that part, just not what you were trying to show with int.MinValue and int.MaxValue You just need to add to the sum variable...
alexhoquan
alexhoquanOP2y ago
the min is the show the lowest value of all the numbers entered then the second loop is just to show up the max value out of all numbers entered
mtreit
mtreit2y ago
Oh you are also getting min and max, as well as sum and average.
alexhoquan
alexhoquanOP2y ago
yea
mtreit
mtreit2y ago
Why would you add the min value to the entered value?
alexhoquan
alexhoquanOP2y ago
for this part?
No description
mtreit
mtreit2y ago
Yes...
alexhoquan
alexhoquanOP2y ago
i think its a static int
mtreit
mtreit2y ago
I don't think you made it static but anyway that's not really relevant to what I was trying to ask. Why are you adding min into the sum?
alexhoquan
alexhoquanOP2y ago
No description
alexhoquan
alexhoquanOP2y ago
No description
alexhoquan
alexhoquanOP2y ago
i guess it didnt work
alexhoquan
alexhoquanOP2y ago
No description
alexhoquan
alexhoquanOP2y ago
nevermind it works now
alexhoquan
alexhoquanOP2y ago
didnt understand the logic on how it worked here .. just my moving the line inside the if
No description
alexhoquan
alexhoquanOP2y ago
also thanks for helping !
mtreit
mtreit2y ago
Sure
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?