Update variable values on repeat loop

Asked

Viewed 127 times

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();
}

2 answers

1

Paul, you are having problems because of this initial balance variable, if I put an initial balance, do not perform any operation, my initial balance will be my final balance... One option, is only to work with the final balance variable, its code becomes simpler, below an example:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Locale.setDefault(Locale.US);
    double saldoFinal = 0, valorSaque, valorDeposito;
    int operacao;

    System.out.println("Digite o saldo inicial: ");
    saldoFinal = 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 += valorDeposito;
            continue;
        case 2:
            System.out.println("Digite o valor do saque: ");
            valorSaque = input.nextDouble();
            saldoFinal -= 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();
}

1


If it does not give the expected result is not working and does not meet the requirement.

I won’t talk about the various problems in this code, nor complain about the use of double for monetary value because it is exercise, but stay on this so you don’t think the code is perfect after you fix the specific problem of the question.

When you make a deposit or a withdrawal is changing the variable saldoFinal based on saldoInicial and more or less the amount entered, that is, no matter how many operations you make it will be done on the final balance without considering the final balance, always consider the initial balance. You didn’t say, but the first operation works, coincidentally, the others go wrong.

In fact the concept is wrong, there is no such thing as starting balance and final balance, balance is balance, stop treating these things as if they were two things and it will happen, the starting balance is just a balance that instead of adding or decreasing is just assigned and then you always operate on the balance and everything works out. If not conceptualizing right the solution ends up going wrong, start disregarding or adding improper "variables" to the problem.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Locale.setDefault(Locale.US);
        int operacao = 0;
        System.out.println("Digite o saldo inicial: ");
        double saldo = 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");
                saldo += input.nextDouble();
                break;
            case 2:
                System.out.println("Digite o valor do saque: ");
                saldo -= input.nextDouble();
                break;
            }
        } while (operacao != 3);
        if (saldo == 0) System.out.println("CONTA ZERADA");
        else if (saldo > 0) System.out.println("CONTA PREFERENCIAL");
        else System.out.println("CONTA ESTOURADA");
        System.out.printf("Saldo da conta: R$ %.2f%n", saldo);
        input.close();
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Stay tuned because I corrected some things I mentioned but not everything.

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

  • 1

    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:

  • Your code breaks if you enter something invalid.

Browser other questions tagged

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