【问题标题】:Bank Account Program Issue银行账户计划问题
【发布时间】:2011-12-07 10:14:32
【问题描述】:

对于我的 Java 类,我们需要创建一个银行帐户,该帐户具有提款、存款和显示当前余额的方法。在 Tester 类中,我想让它询问名称、余额,然后允许您选择 1、2 或 3。然后它会重复您选择的选项,直到您输入“n”。问题在于,运行此代码会导致它在您存入资金后显示“您存入(存入的金额)在帐户(帐户名称)中。您的新余额是(这个)。”它说“这个”的部分与存入的金额完全相同。换句话说,它不添加它,它只是使新余额与存款相同,无论之前有多少。有什么帮助吗?谢谢。

import java.io.*;
import java.util.*;
public class BankAccount
{
    public BankAccount(double b, String n)
    {
        double balance = b;
        String name = n;
    }
    public void deposit(double d)
    {
        balance += d;
    }
    public void withdraw(double w)
    {
        balance -= w;
    }
    public String nickname()
    {
        System.out.print("Enter a new name: ");
        Scanner kbIn = new Scanner(System.in);
        String n = kbIn.nextLine();
        return n;
    }
    double balance;
    String name;
}

还有测试者类:

import java.io.*;
import java.util.*;
public class Tester
{
    public static void main(String args[])
    {
        Scanner kbInLine = new Scanner(System.in);
        Scanner kbIn = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = kbInLine.nextLine();

        System.out.print("Please enter balance: $");
        double balance = kbIn.nextDouble();

        BankAccount myAccount = new BankAccount(balance, name);
        String proceed = "y";

        while(proceed.equalsIgnoreCase("y"))
        {
            System.out.println("\nPlease pick a number. Would you like to...\n\t 1. Deposit\n\t 2. Withdraw\n\t 3. Print Balance\n");
            int choice = kbIn.nextInt();

            switch(choice)
            {
                case 1:
                    System.out.print("How much would you like to deposit?\n\t$");
                    double deposit = kbIn.nextDouble();
                    myAccount.deposit(deposit);
                    System.out.println("You have deposited $" + deposit + " into the account of " + name + ". The new balance is: " + myAccount.balance);
                    break;
                case 2:
                    System.out.print("How much would you like to withdraw?\n\t$");
                    double withdraw = kbIn.nextDouble();
                    if(myAccount.balance - withdraw > 0)
                    {
                        myAccount.withdraw(withdraw);
                        System.out.println("You have withdrawn $" + withdraw + " from the account of " + name + ". The new balance is: " + myAccount.balance);
                    }
                    else    
                    {
                        System.out.println("Sorry, you have insufficient funds for this operation. Your existing balance is $" + myAccount.balance);
                    }
                    break;
                case 3:
                    System.out.print("The balance in the account of " + name + " is $" + myAccount.balance);
                    break;
            }
            System.out.print("\nWould you like to do another transaction? (Y/N)");
            proceed = kbIn.next();
        }
        System.out.println("\nThank you for banking with us. Have a good day!");
    }
}


真正奇怪的是,我在这个项目之前做了一个项目(它实际上是一个简化版本),它存入然后提取一个预定的编码金额,然后输出新的银行余额,并且它做得很好。但是 BankBalance 的代码是一样的。这是那些的代码。
BankAccount 类是:

public class BankAccount
{
    public BankAccount(String nm, double amt) // Constructor
    {
        name = nm;
        balance = amt;
    }
    public void deposit(double d) // Sets up deposit object as balance += d
    {
        balance += d;
    }
    public void withdraw(double w) // Sets up withdraw object as balance -= w
    {
        balance -= w;
    }

    public double balance;
    public String name;
}

Tester 类是:

import java.io.*;
import java.util.*;
public class Tester
{
    public static void main(String args[])
    {
        Scanner kbIn = new Scanner(System.in);
        System.out.print("Enter your name:");
        String name = kbIn.nextLine();

        System.out.print("Enter the balance:");
        double balance = kbIn.nextDouble();

        BankAccount myAccount = new BankAccount(name, balance);
        myAccount.deposit(505.22);
        System.out.println(myAccount.balance);
        myAccount.withdraw(100.00);

        System.out.println("The " + myAccount.name + " account balance is, $" + myAccount.balance);
    }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您实际上并没有在此处初始化您的 balance 成员变量:

    public BankAccount(double b, String n)
    {
        double balance = b;
    

    这将创建一个名为balance 的新本地 变量,您可以将b 的值分配给该变量。在此构造函数运行后,成员变量 balance 将保持为 0(默认值)。

    【讨论】:

    • 是的,他正在创建一个遮蔽类字段的局部变量。
    • 我真的不知道为什么,但是我做了另一个项目(这是它的扩展),它通过存入和提取预定金额来简化这一点,只需说明银行余额.它这样做,但它是完全相同的银行帐户类。我将在上述帖子的更新中发布这些内容。
    • 好的,我想我明白了。在第一个(它不起作用)中,我通过说“double”或“string”来重新初始化我的状态变量。在第二个,它起作用的地方,我只是通过不包括“字符串”来将它们声明为一个值。感谢您的帮助!
    • 对,在第一个中,您正在创建与成员变量同名的 new 局部变量。分配给这些局部变量不会更改成员变量。在第二个中,您将分配给成员变量,这就是您想要的。
    【解决方案2】:

    public BankAccount(double b, String n) { 双平衡= b; 字符串名称 = n; }

    --->

    public BankAccount(double b, String n) { this.balance = b; 这个.name = n; }

    【讨论】:

    • 我不知道“this”是什么意思,我现在正在查找它。但这行得通。谢谢。
    • 另外,知道为什么之前的另一个项目中的代码有效吗? (我更新了我的帖子,所以它显示了旧项目)。
    • 我的英文很差,无法用英文向你解释,对不起。
    【解决方案3】:

    或者您可以将 balance 声明为静态(数据类字段),并将使用此变量的方法也声明为静态。

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2014-08-27
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      相关资源
      最近更新 更多