我的程序有问题。我的问题是我不能减去我从存款金额中提取的款项。

下面的代码:

public static void main(String[] args) {
    double cash;
    boolean more = true;

    Deposite dep = new Deposite();
    Withdraw with = new Withdraw();

    while (more) {
        cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
        dep.Deposite(cash);
        dep.print();


        int con = JOptionPane.YES_NO_OPTION;
     int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?","DEPOSITORY",con);

        if (con1 == 1) {
            int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?","WITHDRAWAL",con);
            if (con3 == 0) {
                cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
                with.Withdraw(cash);
                with.print();
                System.out.println("Thanks");
            }
        }


    }
}


这是我为其功能创建的子类

public class Deposite {
    private double depcash;

        public double Deposite(double cash){
            depcash += cash;

            return this.depcash;
        }
        void print(){
            System.out.printf("Your deposite is $%5.2f",depcash);
            System.out.println(" ");
        }
}


这是我的退学课程。我继承了它。但我仍然不知道它是如何工作的。

下面的代码:

public class Withdraw extends Deposite {
    double cash;

    public double Withdraw(double withdraw){
        super.Deposite(withdraw);
        cash -=withdraw;
        return cash;
    }
    void print (){
        System.out.printf("You Cash Balance now is $%5.2f",cash);
        System.out.println(" ");
    }
}

最佳答案

您的程序有一些基本问题,这里是代码:::
您应该已经创建了用于存款和取款的单一帐户。那是你的基本错误。

import javax.swing.JOptionPane;

public class Bank {
    public static double totalCash = 0;

    public static void main(String[] args) {
        boolean more = true;
        Deposite dep = new Deposite();
        Withdraw with = new Withdraw();
        while (more) {
            double cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Deposite"));
            dep.depositeCash(cash);
            dep.print();
            int con = JOptionPane.YES_NO_OPTION;
            int con1 = JOptionPane.showConfirmDialog(null, "Do you want more Deposites?", "DEPOSITORY", con);
            if (con1 == 1) {
                int con3 = JOptionPane.showConfirmDialog(null, "Withdraw now?", "WITHDRAWAL", con);
                if (con3 == 0) {
                    cash = Double.parseDouble(JOptionPane.showInputDialog("Cash Withdraw"));
                    with.withdrawCash(cash);
                    with.print();
                    System.out.println("Thanks");
                    more = false;
                }
            }
        }
    }
}

class Withdraw {
    public double withdrawCash(double withdraw) {
        Bank.totalCash -= withdraw;
        return Bank.totalCash;
    }

    void print() {
        System.out.printf("You Cash Balance now is $%5.2f", Bank.totalCash);
        System.out.println(" ");
    }
}

class Deposite {
    public double depositeCash(double cash) {
        Bank.totalCash += cash;
        System.out.println(Bank.totalCash);
        return Bank.totalCash;
    }

    void print() {
        System.out.printf("Your deposite is :" + Bank.totalCash);
        System.out.println(" ");
    }
}

07-28 00:56