3
Business rule: A program that receives the code of a user, the Qtd in Kwh q he consumes and the type of it that can be 3: each type multiplies the cost of energy consumed as the constants are defined at the beginning of the program, all this while the user does not enter the code 0, i.e., the program will receive more data while the entered code is not 0. Finally the program should return some things such as the cost to each user ( defined only by each entered code), the total cost for each type inserted by the program until its termination (the program sums up the costs for each type), the cost average for type 2 users and finally the percentage of type 3 users who used the program.
Follows 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 coming out correctly. What is wrong?
Input example: 1, 10, 1, Exit: User account value 1 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 (as a percentage): 0.0%
Next entry: 2, 10, 3, Exit: User account value 2 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 (as a percentage): 0.0%
As in the example above, a user of type 1 was entered and then a user of type 3, my expected output was 0% for the first input and 50% in the second since there were two types and one of them was type 3.
Takes the
f
of 100.0f;– Sorack
still doesn’t work.
– Leko
What exactly is wrong?
– Sorack
What does Voce mean by "not working"? What code should do that does not?
– user28595
it ta returning 0% even when a type 3 user inserts their data.
– Leko
Enter an input example to be able to test the code the way you are doing.
– user28595
I think you’re doing the wrong account, you’re getting 2 / 3 * 100, in fact if you want to divide before multiplying you have to put between parenthesis (2/3) * 100
– Sorack
Aside from you using type int, this invalidates the account
– Sorack
@Sorack between multiplication and division does not need this separation, operations are executed in the order that appears.
– user28595
True, the problem is in the same typing... if you put int it rounds between the first and second operation
– Sorack
@Leko edits the question and explains the problem better, what should I do that does not? So far no one has understood the real problem of the code well.
– user28595
I’ve specified better now
– Leko
I’ll be honest with you, I’m trying to understand what your code does or should do but it’s hard...
– user28595
Really, if you can describe the business rule I think you can help a lot more
– Sorack
Excuse me, I am a beginner and I am working hard to improve. I put the business rule in the question.
– Leko
I think only the change of type already solves, but if you want us
– Sorack
type of which variable should I change?
– Leko