无法更新双倍(余额)

Posted

技术标签:

【中文标题】无法更新双倍(余额)【英文标题】:Cannot get double(balance) to update 【发布时间】:2017-04-24 08:38:36 【问题描述】:

“我正在尝试制作一个银行应用程序,每次您输入存款或取款时都会更新余额。我的菜单和其他一切都运行良好,但在打印出最终余额时,余额始终保持在 5000.00。我哪里做错了?”

            double balance = 5000.00;

                    switch(menu)

                            case 'd': case 'D':
                                double deposit = depositFunds(balance); 
                                break;

                            case 'w': case 'W':
                                double withdrawl = withdrawFunds(balance);
                                break;

                            case 'b': case 'B':
                                checkBalance(accountNumber, balance);
                                break;

                    //end of switch     

    //end of main

     public static double depositFunds(double balance)

        Scanner input = new Scanner(System.in);

        System.out.print("\nEnter the amount of the deposit: ");

        double deposit = input.nextInt();

        double currentBalance = (balance + deposit);

        return currentBalance;

     //end of depositFunds

     public static double withdrawFunds (double balance)

            Scanner input = new Scanner(System.in);

            double currentBalance;

            System.out.print("\nEnter the amount of the withdrawal: ");

            double withdrawal = input.nextInt();

            currentBalance = (balance - withdrawal);

            return currentBalance;

     //end of withdrawFunds

     //Display the balance
     public static void checkBalance(int accountNumber, double balance)

            System.out.printf("\nAccount Number: %d has a current balance of: %.2f\n" , accountNumber , balance);

     //end of checkBalance

【问题讨论】:

您需要将计算值分配给“余额”。你没有改变它 我要提取负数;) 将计算值分配给“余额”是什么意思?我在哪里做呢? 【参考方案1】:

改为这样做:

switch(menu)

                        case 'd': case 'D':
                            balance = depositFunds(balance); 
                            break;

                        case 'w': case 'W':
                            balance = withdrawFunds(balance);
                            break;

                        case 'b': case 'B':
                            checkBalance(accountNumber, balance);
                            break;

             

【讨论】:

哦,好吧!不敢相信我错过了...谢谢!

以上是关于无法更新双倍(余额)的主要内容,如果未能解决你的问题,请参考以下文章

罗宁喊话调整:11日在线hotfix更新汇总

如何按值对 LevelDB 进行排序

Python with peewee to MySQL,保存/更新双倍现有行

无法弄清楚如何保持我的双倍……双倍

锁住余额,为何还会更新异常?

更新mysql中的余额