对于我的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;
}和tester类:
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);
}
}发布于 2011-10-14 10:29:07
您实际上并没有在这里初始化balance成员变量:
public BankAccount(double b, String n)
{
double balance = b;这将创建一个名为balance的新局部变量,并将b值赋给该变量。运行此构造函数后,成员变量balance将保持为0(默认值)。
发布于 2011-10-14 10:40:53
公共BankAccount(double b,String n) { double balance =b,String name = n;}
->
公共BankAccount(double b,String n) { this.balance = b;this.name = n;}
发布于 2012-09-19 10:23:09
或者,您可以将balance声明为static (数据类字段),并将使用此变量的方法也声明为static。
https://stackoverflow.com/questions/7762444
复制相似问题