How to evaluate conditionals in JAVA in this exercise?

1

I have a problem with this exercise. (Attachment enunciated)

The owner of a parking lot requires a program that allows him to determine how much you should charge for the use of parking for your customers. The rates that are had are the following: The first two hours at $ 5.00 each. The next three at $ 4.00 each. The next five at $ 3.00 each. After ten hours the cost for each one is two dollars. Read customer data and parking hours. Present the respective output.

I have evaluated the first condition very well but in the others I have a problem. When I compile and I put him that I need 5 hours of parking he throws me a total of 18 dollars, it is assumed that 5 is included in the second conditional and I must leave 14 as I have put it. The other problem is similar, in the third conditional I need 9 hours of parking and I get 34 when I should leave 17. I hope you can help me, I attach the code.

package estacionamiento;
import java.util.Scanner;


public class Estacionamiento {
public static void main(String[] args) {
    //Declaración de variables tipo cadena, enteras y flotantes//
    String nombrecliente, ced;
    int horas;
    double cobroestacionamiento=0;
    //Ingreso de los Datos//
    Scanner entrada=new Scanner(System.in);
    System.out.println("Digite un nombre y apellido: ");
    nombrecliente=entrada.nextLine();
    System.out.println("Ingrese CI: "); 
    ced=entrada.nextLine();
    System.out.print("¿Por cuantas horas usará el estacionamiento?: ");
    horas = entrada.nextInt();
    //CALCULO CONDICIONAL//
    if(horas<=2)
        cobroestacionamiento=horas*5.00;                
    if(horas>2&&horas<=5)
        cobroestacionamiento=2*5+(horas-3)*4.00;
    if(horas>5&&horas<=10)
        cobroestacionamiento=2*5+3*4+(horas-5)*3.00;
    if(horas>10)
        cobroestacionamiento=2*5+3*4+3*5+(horas-10)*2.00;
    System.out.println("---------DATOS DEL CLIENTE---------");
    System.out.println("Nombres_Cliente:" +nombrecliente);
    System.out.println("Cedula_Cliente:" +ced);
    System.out.println("Sus horas de parqueo son: " +horas+' '+"horas");
    System.out.println("Total a pagar: " +cobroestacionamiento+' '+"dolares");       
}  

}

    
asked by Ballen 02.11.2018 в 01:15
source

2 answers

1

if(horas>2&&horas<=5) cobroestacionamiento=2*5+(horas-3)*4.00;

would be hours -2 since they are 2 hours which cost 10 not 3

if(horas>2&&horas<=5) cobroestacionamiento=2*5+(horas-2)*4.00;

Apart from that you are not doing the calculations well, if the first 2 are worth 10 euros and the next 3 4, the 5 hours would be 22 euros, then I leave the respective outputs of each of the hours and your block of retouched code, greetings

 if(horas>10){

    cobroestacionamiento = 37 + ((horas)-10)*2;

 }else if(horas>5){

    cobroestacionamiento = 22 + ((horas-5)*3);


 }else if(horas>2){


    cobroestacionamiento =  10 + ((horas-2) * 4);

 }else{

   cobroestacionamiento = horas * 5;

      }
    
answered by 02.11.2018 / 09:21
source
0

Use the else clauses, they are necessary there, think about the performance and efficiency of the code. Also rethink the way you condition the number of hours, more or less like this:

if(horas>10) {
  cobroestacionamiento = 2*5+3*4+3*5+(horas-10)*2.00);
} else if(horas>5) {
  cobroestacionamiento = 2*5+3*4+(horas-5)*3.00);
} else if(horas>2) {
  cobroestacionamiento = 2*5+(horas-3)*4.00);
} else {
  cobroestacionamiento = horas*5.00;
}

That way the flow of the program changes significantly.

    
answered by 02.11.2018 в 04:58