5
This code averages the arithmetic or weighted average of the 3 student grades based on your choice of average according to the use of the Equals, code error is exactly this conversion of decimal for int, how to correctly perform this conversion?
using static System.Console;
namespace Função1
{
class Class4
{
static int Main(string[] args)
{
decimal nota1 = 0, nota2 = 0, nota3 = 0;
string letra;
Write("Digite a 1ª nota: ");
nota1 = decimal.Parse(ReadLine());
Write("Digite a 2ª nota: ");
nota2 = decimal.Parse(ReadLine());
Write("Digite a 3ª nota: ");
nota3 = decimal.Parse(ReadLine());
Write("Digite ou A para calcular a média aritmética ou P para calcular a média ponderada: ");
letra = ReadLine();
WriteLine($"{Calcula(nota1, nota2, nota3, letra)}");
ReadKey();
return 0;
}
static int Calcula(decimal nota1, decimal nota2, decimal nota3, string letra)
{
decimal media = 0;
if (letra.Equals("A"))
{
media = (nota1 + nota2 + nota3) / 3;
Write("A opção digitada foi um média aritmética");
}
else if (letra.Equals("P"))
{
media = ((nota1 * 5) + (nota2 * 3) + (nota3 * 2)) / (5 + 3 + 2);
Write("A opção digitada foi um média pnderada");
}
return media;
}
}
}
Carlos, you want the average to be an integer or decimal?
– Daniel Mendes
The average in these cases has to be decimal
– Carlos A.