Validation does not enter the correct "if"

2

My code should have 4 validations and I'm using if and else if , but it'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 value that should enter the second else if which is referring to greater than 700 and less than 1500 it does not do with 1.10% it does as 1.15% and I did a test for it to go to last condition and also will not, it always pick up 1.15%.

The statement of the question is this from here:

  
  • The Tabajara Organizations decided to increase their salaries and hired them to develop the program that will calculate the readjustments. Create 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 completed, please report it 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.
    
  •   
        
    asked by anonymous 05.12.2018 / 20:58

    1 answer

    2

    The biggest problem is that you are using || when the right one would be && . When it falls into the first else if you ask if it is greater than 280. And it is, then it is true, ready, it executes that block and closes. To better understand: What is the difference between the & and & &. .

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

    Predict 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, do you 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}");
        }
    }
    

    See running on .NET Fiddle . And no Coding Ground . Also I put GitHub for future reference .

        
    05.12.2018 / 21:14