为什么不创建一个对象?
有人可以看看我试图废除并关闭帐户的部分代码吗?这是我想达到的目标:“2.添加方法无效close()来您的账户类此方法应该通过附加关闭当前帐户‘为什么不创建一个对象?
CLOSED’帐户名和白平衡设置, 0.(账户号码应保持不变。)也减少账户总数。“
当我试图编译它时,程序给了我一个关于预期标识符的错误。我错过了什么?
//*******************************************************/
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************/
public class Account
{
private double balance;
private String name;
private long acctNum;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//----------------
//Track how many accounts
//----------------
private static int numAccounts=0;
{
numAccounts++;
}
public static int getNumAccounts()
{
return numAccounts;
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns account number.
//----------------------------------------------
public long getAcctNumber()
{
return acctNum;
}
//----------------
//Void and close the accounts
//----------------
public void close(Account)
{
balance = 0;
name = "CLOSE";
acctNum = number;
numAccounts--;
return Account;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
public void close(Account)
需要一个变量名。该声明表示它需要Account
参数,但您还需要为参数指定一个名称,例如public void close(Account account)
。
请注意,我认为这不合理。给定一个账户对象,你应该可以关闭它,所以也许你应该重新考虑这个参数是否有意义。
您不指定参数名称。
将其更改为
public void close(Account acct){
//...
}
public void close(Account)
应该有一个变量名。事情是这样的:
public void close(Account account)
{
balance = 0;
name = "CLOSE";
acctNum = number;
acctNum--;
}
此行必须是一个方法的内部,而不是你的类声明的一部分:
{
numAccounts++;
}
尝试使它:
public void incrementAccountNumber() {
numAccounts++;
}
为什么它是无效的而不是静态的?这些信息与任何特定帐户无关,我只想跟踪有多少帐户在那里。 – 2011-04-11 16:24:11
void是方法的返回类型。如果numAccounts是静态的,这个方法也应该是静态的。如果不是,该方法不应该。无论哪种方式,你没有将该代码定义为一种方法,这就是为什么你会得到一个编译器错误。如果您将其更改为一种方法,您将停止收到错误。 – James 2011-04-11 17:02:50
你需要指定您的参数名称为close()
方法:
public void close(Account account) { ... }
public void close() {
...
}
我认为他们期待您添加一个带有该签名的方法。 此外,它说要追加“CLOSED”到名称不“设置”名称为“CLOSED”。 我不认为你需要用acctNum变量修改任何东西。
那些应该是足够的提示来完成这一点。
而且在其他的答案,你有一个未知变量number
:
acctNum = number;
有几个问题:
public void close(Account) << why do you have Account as a parameter (half parameter)?
{
balance = 0;
name = "CLOSE"; << This is assignment, not addition
acctNum = number; << where number came from
acctNum--;
return Account; << this is a void method, why do you return something?
}
应该看起来像:
public void close()
{
balance = 0;
name += "CLOSE"; << adding "close" to current account name
acctNum--;
}
另一件事,acctNum是帐户的总数,它不应该与特定对象相关,所以应该声明为静态。
编辑
关于账户数和账户号码澄清:
对于每一个客户,应该有一个账号,让我们说第一个帐户具有account number = 1
和第二帐户account number = 2
等。所以我们会打电话给这个变量accountNumber
,并宣布它作为私有变量:
private int accountNumber;
你也想跟踪活跃账户的数量,对吗?所以,你创造出不限定于特定账户,而是与所有的另一个变量,并将其命名为accountCount
,你应该把它声明为静态变量:
private static int accountCount = 0;
当您关闭帐户,你想减少帐户数和保持计数,所以你的方法应该是这样的:
public void close()
{
balance = 0;
name += "CLOSE";
accountCount--; << we decrease the account count, not the specific account number
}
我看到了这个错误,但我很困惑,我试图保持帐号不变,但是改变了帐号的名称,所以它说关闭了,而不是任何人的名字。如果我把整个账户都拿出来,它应该做我想做的事情吗? – 2011-04-11 16:21:38
查看编辑答案。 – MByD 2011-04-11 16:32:44
建议最好去 public void close() { ... }
你会在帐户类的实例调用它(如米yAccount.close()),所以它会在自己的工作。添加你不需要的东西几乎总是一个坏主意:)
编译器是否至少给你一个行号?发布完整的错误消息,它会更有帮助。另外,这功课呢? – FrustratedWithFormsDesigner 2011-04-11 16:14:00
正在做作业吗?它似乎很家庭作业X_X – Neal 2011-04-11 16:14:23
当然它是作业。学校*爱*让学生进行银行账户课程。 :) – 2011-04-11 16:17:11