Percentage in Java

3

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.

    
asked by anonymous 10.11.2016 / 01:27

1 answer

4

I rewrote your problem, because although the error is due to the division of int by int , variable names confuse. Then followed by default put the number for the type just after the name of the variable. I also created the variables only in the scope of use of them. The result was as follows:

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);
  }
}
    
10.11.2016 / 02:39