简体   繁体   English

字符串作为参数

[英]String as a parameter

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the account.
//*******************************************************
import java.text.NumberFormat;

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");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();

   return (acctNum + "\t" + name + "\t" + fmt.format(balance));
    }

  //----------------------------------------------
  // Deducts $10 service fee
  //----------------------------------------------
  public double chargeFee()
  {
    balance=balance-10;
     return balance;
  }

  //----------------------------------------------
  // Changes the name on the account 
  //----------------------------------------------
  public void changeName(String newName)

  {
    name=String.toString(newName);
  }

}

I need help with the last part of this program // Changes name on the account. 我需要该程序最后部分的帮助//更改帐户名称。 I need to make it so that it would take a string (name) as a parameter and change it to a new string(newName), whats is the correct syntax? 我需要使它以字符串(名称)作为参数并将其更改为新的字符串(newName),正确的语法是什么? I couldnt find it in my book. 我在书中找不到它。

name = newName;

will work just fine. 会很好。 String is immutable so it can't be changed afterwards. 字符串是不可变的,因此以后不能更改。

It'd be: 可能是:

public void changeName(String newName)
{
    name=newName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM