Java程序无法正确添加数量

时间:2012-12-11 15:48:01

标签: java methods

所以我还是java的新手。我写过这个,但我不确定为什么当我运行它时,金额不会添加到客户端设置的内容。

例如:我输入500作为起始余额,如果我点击存款,输入500,然后点击案例3,它应该说1000但它仍然说500.然后如果我点击撤销,它说我有 - 500。

有什么想法吗?感谢

package bankaccount;
import java.util.Random;
import java.util.Scanner;

public class Client {
 public static void main(String args[]) {
     Scanner input = new Scanner(System.in);       
     System.out.println("Enter your Name: ");
            String cusName = input.nextLine();
            Random randomGenerator = new Random();
            int accNo = randomGenerator.nextInt(100000);
            System.out.println("Enter Initial Balance: ");
            int balance = input.nextInt();
            BankAccount b1 = new BankAccount(cusName, accNo, balance);
            int menu;
            System.out.println("Menu");
            System.out.println("1. Deposit Amount");
            System.out.println("2. Withdraw Amount");
            System.out.println("3. Display Information");
            System.out.println("4. Exit");
            boolean quit = false;
            do {
                    System.out.print("Please enter your choice: ");
                    menu = input.nextInt();
                    switch (menu) {
                    case 1:            
                             System.out.print("Enter depost amount:");
                             Money.amount = input.nextInt(); 
                             b1.getDeposit(); 

                            break;

                    case 2:
                             System.out.println("Current Account Balance=" + b1.getBalance());
                             System.out.print("Enter withdrawal amount:");
                             Money.amount = input.nextInt();
                             b1.getWithdraw();

                            break;

                    case 3:
                            b1.display();
                            break;
                    case 4:
                            quit = true;
                            break;
                    }
            } while (!quit);
    }

public class Money
{

public static int accountNumber, balance=0;
static int amount;

static String name;
public void setDeposit(int amount) {   

            balance = balance + amount;   
    }
public int getDeposit()
{

            balance = balance + amount;
            if (amount < 0) {
                    System.out.println("Invalid");
            }
                                    return 1; 

}
 public void setBalance(int b)
 {
     b = balance;
 }
 public int getBalance()
 {
     return balance;
 }

 public void setWithdraw(int amount)  {


            balance = balance - amount;
    }
 public int getWithdraw()
 {
            balance = balance - amount;
     if (balance < amount) 
     {
                    System.out.println("Not enough funds.");
                    return 1;
            }
           else if (amount < 0) {
                    System.out.println("Invalid");
                    return 1;}
                    else 
                             return 0;
 }

import java.util.*;

public class BankAccount extends Money {
    static String name;
    public static int balance, amount, acctNum;
    Money customerMoney;


    BankAccount(String name, int accNo, int bal) {
            this.name = name;
            this.acctNum = accNo;
            this.balance = bal;
            this.customerMoney = new Money();
    }
            void display() {
            System.out.println("Name:" + name);
            System.out.println("Account No:" + acctNum);
            System.out.println("Balance:" + balance);

    }

    void displayBalance() {
            System.out.println("Balance:" + balance);
    }
    public Money getMoney(){
          return this.customerMoney;
    }
}

3 个答案:

答案 0 :(得分:4)

最大的问题是声明 balance=0

public static int accountNumber, balance=0;
                                 ^^^^^^^^^

每当您要插入金额时,您的余额为 ZERO

您应该使用setDeposit(input.nextInt())

public void setBalance(int b)中,b = balance;应为balance = b;

此外,您的amountbalance变量应为Float而不是int,因为balance / amount可以是23434.22 }。

答案 1 :(得分:1)

我会删除您正在使用的所有public static int个变量。这些会引起混淆,因为在程序执行时更难以遵循它们的值。最好使用私有变量和公共方法将您的逻辑封装到BankAccount中以修改它们。

我个人会从您的代码中删除Money类。这只会引起混淆,并且不需要简化逻辑。让我们假设账户中有一些任意数量的“钱”,但实际上没有现金支持它 - (有点像现实生活,只是'屏幕上的数字'对吗?) - 在这种情况下我们不会'我需要Moneyint BankAccount balance

如果不尝试对基础功能进行太多更改,我会重写为以下两个类:

BankAccount课程:

package banking;

public class BankAccount {

    /**
     * The balance of this account. <br/>
     * Assumes integer money (Floating point math is horrible and who really
     * needs pesky pence or cents right?!)
     */
    private int balance;
    /**
     * The account number
     */
    private final int acctNum;
    /**
     * Name of the account holder
     */
    private final String name;

    /**
     * Construct our basic account with an account number, account holder and
     * starting balance.
     *
     * @param name
     * @param accNo
     * @param bal
     */
    public BankAccount(String name, int accNo, int bal) {
        this.name = name;
        this.acctNum = accNo;
        this.balance = bal;
    }

    /**
     * Make a deposit to this account by adding a fixed sum to the existing
     * balance. <br/>
     *
     * @param amount
     */
    public void deposit(int amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("You cannot deposit zero or less");
        } else {
            this.balance += amount;
        }
    }

    /**
     * Make a withdrawal from this account by subtracting a fixed amount from
     * the existing balance. <br/>
     *
     * @param amount
     */
    public void withdraw(int amount) {
        if (amount > balance) {
            throw new IllegalArgumentException("Insufficient Funds");
        } else if (amount <= 0) {
            throw new IllegalArgumentException("You cannot withdraw zero or less");
        } else {
            balance -= amount;
        }
    }

    /**
     * Get the account holder name.
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Get the current account balance.
     *
     * @return
     */
    public int getBalance() {
        return balance;
    }

    /**
     * Get the account identifier for this account.
     *
     * @return
     */
    public int getAcctNum() {
        return acctNum;
    }

    /**
     * Debug print method.
     */
    public void display() {
        System.out.println("Name:" + name);
        System.out.println("Account No:" + acctNum);
        System.out.println("Balance:" + balance);
    }
}

主要课程:

package banking;

import java.util.Random;
import java.util.Scanner;

public class BankMain {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your Name: ");
        String customerName = input.nextLine();

        Random randomGenerator = new Random();
        int acctNo = randomGenerator.nextInt(100000);

        System.out.println("Enter Initial Balance: ");
        int balance = input.nextInt();

        BankAccount acct = new BankAccount(customerName, acctNo, balance);

        System.out.println("Menu");
        System.out.println("1. Deposit Amount");
        System.out.println("2. Withdraw Amount");
        System.out.println("3. Display Information");
        System.out.println("4. Exit");

        boolean quit = false;
        int menu;
        do {
            final int transaction;
            System.out.print("Please enter your choice: ");
            menu = input.nextInt();
            switch (menu) {
                case 1:
                    System.out.print("Enter depost amount:");
                    transaction = input.nextInt();
                    acct.deposit(transaction);
                    break;

                case 2:
                    System.out.println("Current Account Balance=" + acct.getBalance());
                    System.out.print("Enter withdrawal amount:");
                    transaction = input.nextInt();
                    try {
                        acct.withdraw(transaction);
                    } catch (IllegalArgumentException iaEx) {
                        System.out.println(iaEx.getMessage());
                    }
                    break;

                case 3:
                    acct.display();
                    break;

                case 4:
                    quit = true;
                    break;
            }
        } while (!quit);
    }
}

这还远非完美,但我觉得更容易理解静态变量和Money类被删除。

答案 2 :(得分:0)

您的代码存在许多问题,例如你的字段都不应该是静态的,但主要的是你有多个重复的字段。

e.g。在Money中你有

public static int accountNumber, balance=0;
static int amount;

static String name;

并在BankAccount中

static String name;
public static int balance, amount, acctNum;
Money customerMoney;

表示您有多个名为namebalanceamount的字段。一些代码使用第一组字段,一些代码使用第二组。您还有customerMoney,但未直接更新。

为避免混淆,我建议您将每个字段设置为非静态且仅在一个位置。删除customerMoney因为您可能想要使用它;)