Percentage in Java

Asked

Viewed 5,807 times

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;

  • still doesn’t work.

  • What exactly is wrong?

  • What does Voce mean by "not working"? What code should do that does not?

  • it ta returning 0% even when a type 3 user inserts their data.

  • Enter an input example to be able to test the code the way you are doing.

  • 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

  • Aside from you using type int, this invalidates the account

  • @Sorack between multiplication and division does not need this separation, operations are executed in the order that appears.

  • True, the problem is in the same typing... if you put int it rounds between the first and second operation

  • @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.

  • I’ve specified better now

  • 1

    I’ll be honest with you, I’m trying to understand what your code does or should do but it’s hard...

  • 1

    Really, if you can describe the business rule I think you can help a lot more

  • Excuse me, I am a beginner and I am working hard to improve. I put the business rule in the question.

  • I think only the change of type already solves, but if you want us

  • type of which variable should I change?

Show 12 more comments

1 answer

4


I rewrote your problem because although the error is due to division of int for int,the variable names confused. Then I followed as default put the number referring to the type right after the variable name. I also created variables only in their scope of use. The result was the following:

import java.util.Scanner;

public class Energia {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int codigo;

    int quantidade1 = 0;
    int quantidade2 = 0;
    int quantidade3 = 0;

    double multiplicador1 = 1.5;
    double multiplicador2 = 3;
    double multiplicador3 = 5;

    double total1 = 0;
    double total2 = 0;
    double total3 = 0;

    do {
      double consumido;
      int tipo;
      double custoTotal = 0;
      double quantidadeTotal;
      double porcentagem3;
      double media2 = 0;

      System.out.println("Digite o código do consumidor: ");
      codigo = sc.nextInt();

      System.out.println("Digite a quantidade consumida em KWh: ");
      consumido = sc.nextDouble();

      System.out.println("Digite o tipo de consumidor: ");
      tipo = sc.nextInt();

      switch (tipo) {
        case 1:
          quantidade1 = quantidade1 + 1;
          custoTotal = multiplicador1 * consumido;
          total1 = total1 + custoTotal;
          break;
        case 2:
          quantidade2 = quantidade2 + 1;
          custoTotal = multiplicador2 * consumido;
          total2 = total2 + custoTotal;
          break;
        case 3:
          quantidade3 = quantidade3 + 1;
          custoTotal = multiplicador3 * consumido;
          total3 = total3 + custoTotal;
          break;
      }

      System.out.println("Valor da conta do usuário " + codigo + " é: " + custoTotal);

      quantidadeTotal = quantidade1 + quantidade2 + quantidade3;
      media2 = total2 / quantidade2;
      porcentagem3 = quantidade3 / quantidadeTotal * 100;

      System.out.println("Total tipo 1: " + total1);
      System.out.println("Total tipo 2: " + total2);
      System.out.println("Total tipo 3: " + total3);
      System.out.println("Media custo tipo 2: " + media2);
      System.out.println("Tipo 3 (em porcentagem): " + porcentagem3 + "%");
    } while (codigo != 0);
  }
}
  • switch instead of if was what fixed the problem?

  • IDE suggestion. When if compares only one variable value the switch becomes more suitable because it is more readable

  • Did it work? In a positive case confirm in "V" so that the answer serves other people with similar doubt

  • 1

    I made your code by if and it worked this time,which seems to start some variables inside of...while fixed the problem even without me understanding why. if it’s not that I can’t say what the mistake is.

  • The type of variables ^^

Browser other questions tagged

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