java - 为什么这没有创建一个对象?

标签 java

有人可以看看我的代码中我试图作废并关闭帐户的部分吗?这就是我想要实现的目标:

"2. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts."

当我尝试编译它时,程序给出了一个关于需要标识符的错误。我错过了什么?

//*******************************************************/
// 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(帐户帐户)

请注意,我认为这没有多大意义。给定一个帐户对象,您可能应该能够关闭它,因此也许您应该重新考虑该参数是否有意义。

关于java - 为什么这没有创建一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624239/

相关文章:

java - 我应该如何编码我的java,以便提交按钮仅在android应用程序中选择复选按钮后才处于 Activity 状态

java - java和php中的时间戳有什么区别?

java - 文件未使用 POST 请求正确上传

Java 缓冲写入器

java - 如何在 Java 中将字符串中的非常大的二进制转换为八进制

java - 为什么 Integer 类缓存值在 -128 到 127 范围内?

java - 不要重复 DAO!使用通用 DAO

java - 使用 Observable 单例类来处理网络调用不好吗?

java - 使用 htmlparser 解析网站时无法获取所有匹配的节点

java - 无法构造 java.util.LinkedHashMap : no String-argument constructor/factory 的实例