Compound interest with switch and [preferably] no break in each case

Asked

Viewed 954 times

0

Hello. I have the table below, which calculates interest on interest [0.5% per month, cumulative] and I would like to do it with switch and [preferably] without break in each case. I also wanted the case to start with the last installment (case 12:, in the case).

par-    valor 
cela   a pagar
1    R$ 100,00 
2    R$ 105,00 
3    R$ 110,25 
4    R$ 115,76 
5    R$ 121,55 
6    R$ 127,63 
7    R$ 134,01 
8    R$ 140,71 
9    R$ 147,75 
10   R$ 155,13 
11   R$ 162,89 
12   R$ 171,03 

I came up with a very "rustic" solution [not to say "idiot"], which solved the problem, but which became too stony:

import java.util.Scanner;

public class SwitchExercicio003 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner leitor = new Scanner(System.in);
    System.out.println("Qual parcela você está pagando? De 1 a 12");
    int parcela = leitor.nextInt();
    double valor = 100.00;
    double taxa = 1.05;
    double case2 = valor * taxa; 
    double case3 = case2 * taxa;
    double case4 = case3 * taxa;
    double case5 = case4 * taxa;
    double case6 = case5 * taxa;
    double case7 = case6 * taxa;
    double case8 = case7 * taxa;
    double case9 = case8 * taxa;
    double case10 = case9 * taxa;
    double case11 = case10 * taxa;
    double case12 = case11 * taxa;

    switch (parcela) {
    case 1:
        System.out.println("Você deve pagar R$ " + valor);
        break;
    case 2:
        System.out.println("Você deve pagar R$ " + case2);
        break;
    case 3:
        System.out.println("Você deve pagar R$ " + case3);
        break;
    case 4:
        System.out.println("Você deve pagar R$ " + (case4));
        break;
    case 5:
        System.out.println("Você deve pagar R$ " + (case5));
        break;
    case 6:
        System.out.println("Você deve pagar R$ " + (case6));
        break;
    case 7:
        System.out.println("Você deve pagar R$ " + (case7));
        break;
    case 8:
        System.out.println("Você deve pagar R$ " + (case8));
        break;
    case 9:
        System.out.println("Você deve pagar R$ " + (case9));
        break;
    case 10:
        System.out.println("Você deve pagar R$ " + (case10));
        break;
    case 11:
        System.out.println("Você deve pagar R$ " + (case11));
        break;
    case 12:
        System.out.println("Você deve pagar R$ " + (case12));
        break;
    default:
        System.out.println("Oops, digite um número entre 1 e 12!");
    }

}

}

The teacher said that I could do exactly as I mentioned in the title of the post, but I couldn’t get anywhere near it. Thank you in advance for your help. Hug.

  • 2

    Why would I want to do with switch? Wouldn’t it be better to do it the right way? You’re having some specific difficulty?

  • The fact is that I’ve only been programming for 1 month. This exercise is much more logical within the switch than of logic only. It was a challenge of the teacher.

  • But to "learn" to use a logic where one should not use this logic is to unlearn.

2 answers

1

It would be easier to create a method to calculate the interest, follows example code:

public class TesteJuros {
    public static void main(String[] args) {
        for (int i = 0; i < 12; i++) {
            System.out.println(String.format("%.2f", calcularJuros(5, i, 100)));
        }
    }

    public static double calcularJuros(double taxa, int mesesDecorridos, double valor) {
        // formula para calculo de juros compostos
        double multiplicador = Math.pow(1.0 + taxa / 100.0, mesesDecorridos) - 1.0;
        return valor + multiplicador * valor;
    }
}

Abcs!

  • 1

    Very cool. I think I get it, but "officially" I haven’t learned the for. Anyway, it seemed easier to use this method mainly if it is within a larger code, which combines other operations with interest and such. Thank you!

0

switch (parcela) {
    default:
        System.out.println("Oops, digite um número entre 1 e 12!");
        break
    case 12:
        valor *= taxa;
    case 11:
        valor *= taxa;
    case 10:
        valor *= taxa;
    case 9:
        valor *= taxa;
    case 8:
        valor *= taxa;
    case 7:
        valor *= taxa;
    case 6:
        valor *= taxa;
    case 5:
        valor *= taxa;
    case 4:
        valor *= taxa;
    case 3:
        valor *= taxa;
    case 2:
        valor *= taxa;
    case 1:
        System.out.println("Você deve pagar R$ " + valor);
}

and if you really need to solve it without break:

switch (parcela) {
    default:
        valor = 0.0
    case 12:
        valor *= taxa;
    // ...
    case 2:
        valor *= taxa;
    case 1:
        if (valor > 0.0) {
            System.out.println("Você deve pagar R$ " + valor);
        } else {
            System.out.println("Oops, digite um número entre 1 e 12!");
        }
}
  • Bingo! That’s what it was. I was looking at Excel and thinking "there must be some operation that does exactly what it is doing right now". It was a simple *=. Thank you very much!

  • I sent this answer because your problem asked for a solution using switch, but if that’s not a restriction you should really use the compound interest formula @hlucasfranca posted.

Browser other questions tagged

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