Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 银行账户计划问题_Java - Fatal编程技术网

Java 银行账户计划问题

Java 银行账户计划问题,java,Java,对于我的Java类,我们需要创建一个银行帐户,该帐户具有取款、存款和显示当前余额的方法。在Tester类中,我想让它询问名称、余额,然后允许您选择1、2或3。然后它重复您选择的选项,直到您说键入“n”。问题是,运行此代码会导致在您存款后显示“您在帐户(帐户名称)中存款(存款金额)。您的新余额是(this)。”其中显示“this”的部分与存款金额完全相同。换言之,它并没有加上它,它只是使新的余额与存款相同,不管以前存了多少。有什么帮助吗?谢谢 import java.io.*; import ja

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

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

真正有趣的是,在这之前我做了一个项目(实际上是一个简化版本),它先存款,然后提取预先确定的编码金额,然后输出新的银行余额,这样做很好。但银行余额的代码是相同的。这是代码。
银行账户类别为:

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;
}
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);
    }
}
测试仪类别为:

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

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

public BankAccount(double b, String n)
{
    double balance = b;
这将创建一个名为
balance
的新局部变量,您将
b
的值分配给该变量。运行此构造函数后,成员变量余额将保持为0(默认值)

公共银行账户(双b,字符串n) { 双平衡=b; 字符串名=n; }

--->

公共银行账户(双b,字符串n) { 这个平衡=b; this.name=n;
}

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

是的,他正在创建一个局部变量来隐藏类字段。我真的不知道为什么,但是我做了另一个项目(这是它的扩展)通过存款和提取预定金额,并仅说明银行余额,简化了这一过程。它可以这样做,但它是完全相同的bankaccount类。我会在上面的更新中发布这些信息。好的,我想我知道了。在第一个例子中(它不起作用),我通过说“double”或“string”来重新初始化我的状态变量。在第二个应用程序中,我只是通过不包含“String”将它们声明为一个值。谢谢你的帮助!对,在第一个例子中,您创建了与成员变量同名的新局部变量。分配给这些局部变量不会更改成员变量。在第二个示例中,您将分配给成员变量,这是您想要的。我不知道“this”是什么意思,我现在正在查找它。但这起了作用。谢谢。还有,你知道以前的项目中的代码为什么有效吗?(我更新了我的帖子,所以它显示了旧的项目)。我的英语很差,所以我不能用英语向你解释。对不起。