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.
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?– Maniero
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.– Sandro Brincher
But to "learn" to use a logic where one should not use this logic is to unlearn.
– Maniero