对于我的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);
}
}
答案 0 :(得分:4)
您实际上并未在此处初始化balance
成员变量:
public BankAccount(double b, String n)
{
double balance = b;
这会创建一个名为balance
的新本地变量,并为其指定b
的值。运行此构造函数后,成员变量余额将保持为0(默认值)。
答案 1 :(得分:1)
public BankAccount(double b,String n) { 双重余额= b; 字符串名称= n; }
--->
public BankAccount(double b,String n) { this.balance = b; this.name = n; }
答案 2 :(得分:0)
或者您可以将balance声明为static(数据类字段),以及将此变量用作static的方法。