有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

if语句Java银行程序如何从余额中扣除

我有一个简单的银行程序,你可以检查你的余额,存款和取款。顺便说一下,这是一个循环,因此您可以进行多个事务

char anotherTransact, option;
    int balance;
    double deposit, withdraw;
    
    
    do {
    System.out.println("\nWelcome to ABC BANK \n");
    
    System.out.println("B - Check for Balance");
    System.out.println("D - Make Deposit");
    System.out.println("W - Make Withdraw");
    System.out.println("Q - Quit");
    
    
        System.out.print("\nSelect an option : ");
        option = scan.next().charAt(0);
        
        
        balance = 100000; 
        if ((option == 'B') || (option == 'b')) {
            System.out.println("\nYour current balance is " +balance);
        }
        
        else if ((option == 'D') || (option == 'd')) {
            System.out.print("\nEnter amount to Deposit : ");
            deposit = scan.nextDouble();
            if (deposit > 1) {
                System.out.println("Deposit Transaction is successfully completed.");
                double newBalance = 100000 + deposit; 
            }
            else if (deposit > 500000) {
                System.out.println("Deposit Amount must not be greater than 500,000");
            }
            else {
                System.out.println("Deposit must be greater than zero");
            }
        }
        
        else if ((option == 'W') || (option == 'w')) {
            System.out.print("\nEnter amount to Withdraw : ");
            withdraw = scan.nextDouble();
            if (withdraw % 100 == 0) {
                System.out.println("Withdrawal Transaction is successfully completed.");
                
            }
            else if (withdraw > 150000) {
                System.out.println("Withdrawal Amount must not be greater then 150,000");
            }
            else {
                System.out.println("Withdrawal Amount must be greater than zero");
            }
        }
        
        else {
            System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
        }
        
        System.out.print("\nWant to Transact another (Y/N?) ");
        anotherTransact = scan.next().charAt(0);        
    }
    
    while ((anotherTransact == 'Y') || (anotherTransact =='y'));

问题是,当我选择取款选项时,我不知道它应该如何在下一次银行交易中扣除余额,并在余额中添加存款

比如说,我会在银行存40美元。然后做一笔新的交易,打开我的余额,将我存入的40美元加起来。我怎样才能把存款加到我的余额中呢?我取款时如何扣除


共 (3) 个答案

  1. # 1 楼答案

    首先,从余额中扣除只是balance -= withdrawal,这与balance = balance - withdraw相同,这将更新变量balance。当您想要打印currentnt余额时,只需调用此变量
    double newBalance = 100000 + deposit;不会更新您的余额,因为您只打印余额而不是新余额
    其次,balance不应该是一个int变量
    您的一些交易规则不正确。支取时,应检查余额是否大于支取金额,支取金额是否大于0(balance > withdraw && withdraw > 0)。另外,withdraw % 100 == 0对我来说没有意义,它不是"Withdrawal Amount must be greater than zero"的条件

  2. # 2 楼答案

    在会计中,您不跟踪任何余额值,因为它总是动态计算的
    在资产负债表(通常是数据库表)中,存款是正值(>;0),取款是负值(<;0)。如果使用类似于intdouble的数据类型。。。你只能代表美分;通常BigDecimal被用来表示货币价值

    如果您想让这个程序以某种方式工作,请将所有事务推送到ArrayList中。我的意思是,一个愚蠢的问题是:你打算如何在不跟踪所有交易的情况下发布银行对账单

  3. # 3 楼答案

    您需要更新balance变量。您在循环的每次迭代中将balance设置为100000。另外,我把它改为扫描。因为余额是一个整数。如果您想处理小数,请将balance更改为double,并将nextInt返回nextDouble

    import java.util.Scanner;
    
    class Main {  
      public static void main(String args[]) { 
        char anotherTransact, option;
        int balance = 100000;
        double deposit, withdraw;
        Scanner scan = new Scanner(System.in);
        
        do {
        System.out.println("\nWelcome to ABC BANK \n");
        
        System.out.println("B - Check for Balance");
        System.out.println("D - Make Deposit");
        System.out.println("W - Make Withdraw");
        System.out.println("Q - Quit");
        
        
            System.out.print("\nSelect an option : ");
            option = scan.next().charAt(0);
            
            
            if ((option == 'B') || (option == 'b')) {
                System.out.println("\nYour current balance is " +balance);
            }
            
            else if ((option == 'D') || (option == 'd')) {
                System.out.print("\nEnter amount to Deposit : ");
                deposit = scan.nextInt();
                if (deposit > 1) {
                    System.out.println("Deposit Transaction is successfully completed.");
                    balance += deposit; 
                }
                else if (deposit > 500000) {
                    System.out.println("Deposit Amount must not be greater than 500,000");
                }
                else {
                    System.out.println("Deposit must be greater than zero");
                }
            }
            
            else if ((option == 'W') || (option == 'w')) {
                System.out.print("\nEnter amount to Withdraw : ");
                withdraw = scan.nextInt();
                if (withdraw % 100 == 0) {
                    balance -= withdraw;
                    System.out.println("Withdrawal Transaction is successfully completed.");
                    
                }
                else if (withdraw > 150000) {
                    System.out.println("Withdrawal Amount must not be greater then 150,000");
                }
                else {
                    System.out.println("Withdrawal Amount must be greater than zero");
                }
            }
            
            else {
                System.out.println("\nInvalid entry, enter any valid option : (B/D/W/Q)");
            }
            
            System.out.print("\nWant to Transact another (Y/N?) ");
            anotherTransact = scan.next().charAt(0);        
        }
        
        while ((anotherTransact == 'Y') || (anotherTransact =='y')); 
      } 
    }