0
I ran a simple cash-and-deposit operation code on a variable. The code works and meets withdrawal and deposit operations normally, it happens that when I want to do a new operation, the value that returns me is the same as the starting balance, not the value update.
For example, if I put initial balance 1000, type 2 to withdraw and put 500, the updated value should return me 500 and perform the calculation on top of these 500, but in the new operation #2 (draw), it removes the 500 of the initial variable (1000)and gives me a wrong number because of it. I mean, call me back 1000 - 500 = R$ 500,00 and the right thing should be 1000 - 500 - 500 = R$ 0,00.
This value could not already be automatically updated in the command continue
, that returns to the loop?
Just follow my code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Locale.setDefault(Locale.US);
double saldoInicial, saldoFinal = 0, valorSaque, valorDeposito;
int operacao;
System.out.println("Digite o saldo inicial: ");
saldoInicial = input.nextDouble();
do {
System.out.println("Digite o código da operação (1-deposito, 2-saque, 3-encerrar): ");
operacao = input.nextInt();
switch (operacao) {
case 1:
System.out.println("Digite o valor a ser depositado");
valorDeposito = input.nextDouble();
saldoFinal = saldoInicial + valorDeposito;
continue;
case 2:
System.out.println("Digite o valor do saque: ");
valorSaque = input.nextDouble();
saldoFinal = saldoInicial - valorSaque;
continue;
}
} while (operacao != 3);
if (saldoFinal == 0) {
System.out.println("CONTA ZERADA");
} else if (saldoFinal > 0) {
System.out.println("CONTA PREFERENCIAL");
} else {
System.out.println("CONTA ESTOURADA");
}
System.out.printf("Saldo da conta: R$ %.2f%n", saldoFinal);
input.close();
}
When he arrives talking in the face "the various problems of the code" can sound a little aggressive for those who were from scratch in their own effort, in the nail of the code, achieve the expected result. The way introduced seemed that the code was in Narnia and completely incorrect, but I saw only adjustments allied to the solution :). Apart from the question of Double and the monetary factor, could you point out to me what other problems would that be? But improvement and improvement is, really. Even thank you for the touches, as for example the direct calculation in the input, in which I had not yet known.
– Paul Heinrich
Each one can interpret as they wish, I could not say anything, you think everything is in order and spend the rest of your life deluding yourself, some people like this, others prefer to learn how to do it right, I give preference to who is of the second case, But since I don’t know everyone, I don’t have to select who to answer to. There would be no space to list every detail and explain it properly, some things involve more advanced and complicated concepts, and we usually solve problems on time, I just warn people not to believe that everything is okay.. Did you see I fixed some? Another:
– Maniero
Your code breaks if you enter something invalid.
– Maniero