Validation does not enter the correct "if"

Asked

Viewed 104 times

2

My code should have 4 validations and I’m using if and else if, but he’s only doing two of them, see the code below:

double salario = 0;
        double p0 = 1.20;
        double p1 = 1.15;
        double p2 = 1.10;
        double p3 = 1.05;

        Console.WriteLine("Digite o valor do salário do funcionário: ");
        salario = double.Parse(Console.ReadLine());

        if (salario == 280) {
            salario = (salario * p0);
            Console.WriteLine("O salário do funcionário é: " + salario);
            Console.WriteLine("O percentual de aumento aplicado foi: " + p0);
        }
        else if (salario >= 280 || salario <= 700)
        {
            salario = (salario * p1);
            Console.WriteLine("O salário do funcionário é: " + salario);
            Console.WriteLine("O percentual de aumento aplicado foi: " + p1);
        }
        else if (salario >= 700 || salario <= 1500)
        {
            salario = (salario * p2);
            Console.WriteLine("O salário do funcionário é: " + salario);
            Console.WriteLine("O percentual de aumento aplicado foi: " + p2);
        }
        else
        {
            salario = (salario * p3);
            Console.WriteLine("O salário do funcionário é: " + salario);
            Console.WriteLine("O percentual de aumento aplicado foi: " + p3);
        }

        Console.WriteLine("Pressione ENTER para sair...");
        Console.Read();

When I put a salary amount that should go into the second else if which refers to more than 700 and less than 1500 it does not with 1.10% it does as being 1.15% and I did a test for it to go to last condition and also will not, it always catch 1.15%.

The statement of the question is this:

  1. The Tabajara Organizations decided to give a salary increase to their employees and hired them to develop the program that will calculate the adjustments. Prepare a routine that receives the salary of a collaborator and the adjustment according to the following criterion, based on the current salary: (1,0 point)

    a.    Salários até R$ 280,00 (incluindo):  aumento de 20%;
    b.    Salários entre R$ 280,00 e R$ 700,00: aumento de 15%;
    c.    Salários entre R$ 700,00 e R$ 1500,00: aumento de 10%;
    d.    Salários de R$ 1500,00 em diante: aumento de 5%.
    

    After the increase is performed, inform on the screen:

    a.    O salário antes do reajuste;
    b.    O percentual de aumento aplicado;
    c.    O valor do aumento;
    d.    O novo salário, após o aumento.
    
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points, you will have it right after you accept it).

1 answer

3

The biggest problem is that you’re using || when the right one would be &&. When I fall on the first else if you ask if it’s bigger than 280. And it is, so it’s true, ready, it runs this block and closes. To better understand: What is the difference between & and &&?.

The code has other problems, I’ll show you some. One of them is use double for monetary value.

You need to test if the data was entered correctly, otherwise it will break. I just returned without doing anything, you can sophisticate and say an error message or even ask to type again. The rest is more cosmetic that gets better and simpler, agree?

using static System.Console;

public class Program {
    public static void Main() {
        var p0 = 1.20M;
        var p1 = 1.15M;
        var p2 = 1.10M;
        var p3 = 1.05M;
        WriteLine("Digite o valor do salário do funcionário: ");
        if (!decimal.TryParse(ReadLine(), out var salario)) return; //deu erro
        decimal perc;
        if (salario < 280) perc = p0;
        else if (salario >= 280 && salario < 700) perc = p1;
        else if (salario >= 700 && salario < 1500) perc = p2;
        else perc = p3;
        WriteLine($"O salário do funcionário é: {salario *= perc}");
        WriteLine($"O percentual de aumento aplicado foi: {perc}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Browser other questions tagged

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