Run user input on a given number

Asked

Viewed 53 times

-2

I am making a program that calculates a final note by summing the numbers and dividing them by 4. I would like to add the function of typing as many notes as the users want.

This is the code I have at the moment:

        int num0;
        int num1;
        int num2;
        int num3;
        int sum;
        int div;
        String input0 = "";
        String input1 = "";
        String input2 = "";
        String input3 = "";
        Console.WriteLine("Calculador de média escolar\n");
        Console.WriteLine("Por favor, digite o primeiro número:");
        input0 = Console.ReadLine();
        Console.WriteLine("Por favor, digite o segundo número:");
        input1 = Console.ReadLine();
        Console.WriteLine("Por favor, digite o terceiro número:");
        input2 = Console.ReadLine();
        Console.WriteLine("Por favor, digite o quarto número:");
        input3 = Console.ReadLine();
        num0 = Convert.ToInt32(input0);
        num1 = Convert.ToInt32(input1);
        num2 = Convert.ToInt32(input2);
        num3 = Convert.ToInt32(input3);
        sum = num0 + num1 + num2 + num3;
        div = sum / 4;
        Console.WriteLine("A nota é: " +div);
        Console.ReadLine();

I’d appreciate the help.

1 answer

-1

One option is to ask the user how many notes he wants to type, so just make one for on top of that amount:

int notaTotal = 0;
int qtdeNotas;
int div;

Console.WriteLine("Calculador de média escolar\n");
Console.WriteLine("Por favor, digite quantas notas você tem:");
qtdeNotas = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= qtdeNotas; i++){
    Console.WriteLine("Por favor, digite a {0}ª nota:", i);
    notaTotal += Convert.ToInt32(Console.ReadLine());
}
div = notaTotal / qtdeNotas;
Console.WriteLine("A nota é: " +div);
Console.ReadLine();
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.