gpt4 book ai didi

java - 银行账户程序问题

转载 作者:行者123 更新时间:2023-11-30 06:33:38 28 4
gpt4 key购买 nike

对于我的 Java 类,我们需要创建一个银行帐户,该帐户具有取款、存款和显示当前余额的方法。在 Tester 类中,我想让它询问姓名、余额,然后让你选择 1、2 或 3。然后它重复你选择的选项,直到你说键入“n”。问题是运行这段代码会导致它在你存钱后说“你存入了(存入的金额)账户(账户名称)。你的新余额是(这个)。”它说“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!");
}
}


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

最佳答案

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

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

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

关于java - 银行账户程序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762444/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com