java 银行账户计划问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7762444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:18:50  来源:igfitidea点击:

Bank Account Program Issue

java

提问by Alex G

For my Java class, we need to make a bank account that has the methods of withdrawing, depositing money, and displaying current balance. In the Tester class, I want to make it ask for the name, the balance, then allow you to choose 1, 2, or 3. Then it repeats the option you choose until you say type "n". The problem is that running this code causes it to say after you deposit money "You deposited (amount of money deposited) in the account (name of account). Your new balance is (this)." The part where it says "this" is the exact same of the amount of money deposited. In other words, it doesn't add it, it just makes the new balance the same as the deposit, regardless of how much was in before. Any help? Thanks.

对于我的 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;
}

And the tester class:

和测试员类:

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!");
    }
}


What's really wierd is that I did a project before this one (it's actually a simplified version) where it deposits and then withdraws a predetermined, coded amount, then outputs the new bank balance, and it does it fine. But the code for BankBalance is the same. Here's the code for those.
BankAccount class is:


真正奇怪的是,我在此之前做过一个项目(它实际上是一个简化版本),它存入然后提取预定的编码金额,然后输出新的银行余额,并且它做得很好。但 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;
}

And the Tester class is:

而 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);
    }
}

回答by Greg Hewgill

You're not actually initialising your balancemember variable here:

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

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

This creates a new localvariable called balance, to which you assign the value of b. The membervariable balance will remain 0 (the default) after this constructor is run.

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

回答by u993109

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

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

--->

--->

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

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

回答by user1207965

Or you can declare balance as static (data class field), and the methods that use this variable as static too.

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