Business rule : A program that receives the code of a user, the qtd in KWh that it consumes and the type of it that can be 3: each type multiplies the cost of energy consumed as they are defined the constants at the beginning of the program, all this while the user does not enter the code 0, that is, the program receives more data as long as the code entered is not 0. Finally the program must return some things like the cost for each user (defined solely by each inserted code), the total cost for each type inserted by the program until its closure (the program adds the costs for each type), the average cost for type 2 users and finally the percentage of users of the program. type 3 that used the program.
Follow the code:
public class Problema01 {
static double tipo1 = 1.5, tipo2 = 3.0, tipo3 = 5.0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int entrada, tipo, codigo, qtd0 = 0, qtd = 0, qtd1 = 0, qtdTotal = 0;
double porcentagem = 0, consumido, custoTotal, soma0 = 0, soma = 0, soma1 = 0, media = 0;
do {
System.out.println("Digite o código do consumidor: ");
codigo = sc.nextInt();
System.out.println("Digite a quantidade consumida em KWh: ");
consumido = sc.nextInt();
System.out.println("Digite o tipo de consumidor: ");
tipo = sc.nextInt();
if (tipo == 1) {
qtd0 = qtd0 + 1;
custoTotal = tipo1 * consumido;
soma0 = soma0 + custoTotal;
System.out.println("Valor da conta do usuário " + codigo + " é: " + custoTotal);
} else if (tipo == 2) {
qtd = qtd + 1;
custoTotal = tipo2 * consumido;
soma = soma + custoTotal;
media = soma / qtd;
qtdTotal = qtd0 + qtd + qtd1;
System.out.println("Valor da conta do usuário " + codigo + " é: " + custoTotal);
} else if (tipo == 3) {
qtd1 = qtd1 + 1;
custoTotal = tipo3 * consumido;
soma1 = soma1 + custoTotal;
System.out.println("Valor da conta do usuário " + codigo + " é: " + custoTotal);
}
qtdTotal = qtd0 + qtd + qtd1;
porcentagem = qtd1 / qtdTotal * 100.0f;
System.out.println("Total tipo 1: " + soma0);
System.out.println("Total tipo 2: " + soma);
System.out.println("Total tipo 3: " + soma1);
System.out.println("Media custo tipo 2: " + media);
System.out.println("Tipo 3 (em porcentagem): " + porcentagem + "%");
} while (codigo != 0);
}
}
The last output asks for the percentage of type 3 users but is not outputting correctly. What's wrong?
Example input: 1, 10, 1, Exit: User 1's account value is: 15.0, Total Type 1: 15.0, Total type 2: 0.0, Total type 3: 0.0, Average cost type 2: 0.0, Type 3 (in percentage): 0.0%
Next entry : 2, 10, 3, Exit : User 2's account value is: 50.0, Total Type 1: 15.0, Total type 2: 0.0, Total type 3: 50.0, Average cost type 2: 0.0, Type 3 (in percentage): 0.0%
As in the example above, a user of type 1 and then a user of type 3, my expected output was 0% for the first entry and 50% for the second, since there were two types, one of them being type 3.