R
R
Redrik052021-08-27 18:00:59
Java
Redrik05, 2021-08-27 18:00:59

Why is the variable change not saved?

Good afternoon. The saveExpense method is supposed to return the modified value of the moneyBeforeSalary variable, but after each iteration of moneyBeforeSalary, it returns to its original value.

Please help. Thanks in advance!

the code:

public static void main(String[] args) {
        double[] expenses = new double[7];

        Scanner scanner = new Scanner(System.in);

        System.out.println("Сколько денег у вас осталось до зарплаты?");
        double moneyBeforeSalary = scanner.nextDouble();

        System.out.println("Сколько дней до зарплаты?");
        int daysBeforeSalary = scanner.nextInt();

        while (true) {
            printMenu();
            int command = scanner.nextInt();
            if (command == 1) {
                convert(scanner, moneyBeforeSalary);
            } else if (command == 2) {
                getAdvice(moneyBeforeSalary, daysBeforeSalary);
            } else if (command == 3) {
                saveExpense(scanner, moneyBeforeSalary, expenses);
            } else if (command == 4) {
                printAllExpenses(expenses);
            } else if (command == 5) {
                System.out.println("Самая большая сумма расходов на этой неделе составила " + findMaxExpense(expenses) + " руб.");
            } else if (command == 0) {
                System.out.println("Выход");
                break;
            } else {
                System.out.println("Извините, такой команды пока нет.");
            }
        }
    }


    public static double saveExpense(Scanner scanner, double moneyBeforeSalary, double[] expenses) {
        System.out.println("За какой день вы хотите ввести трату: 1-ПН, 2-ВТ, 3-СР, 4-ЧТ, 5-ПТ, 6-СБ, 7-ВС?");
        int day = scanner.nextInt();
        System.out.println("Введите размер траты:");
        double expense = scanner.nextDouble();
        moneyBeforeSalary -= expense;
        expenses[day - 1] = expenses[day - 1] + expense;
        System.out.println("Значение сохранено! Ваш текущий баланс в рублях: " + moneyBeforeSalary);
        
        if (moneyBeforeSalary < 1000) {
            System.out.println("На вашем счету осталось совсем немного. Стоит начать экономить!");
        }

        return moneyBeforeSalary;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dewerro, 2021-08-27
@Redrik05

Change
saveExpense(scanner, moneyBeforeSalary, expenses);
the line: to:

moneyBeforeSalary = saveExpense(scanner, moneyBeforeSalary, expenses);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question