首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串作为参数

字符串作为参数
EN

Stack Overflow用户
提问于 2011-04-05 03:18:40
回答 2查看 2.7K关注 0票数 0
代码语言:javascript
复制
//*******************************************************
// 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);
  }

}

我需要在此程序的最后部分//更改帐户名称方面的帮助。我需要让它接受一个字符串(名称)作为参数,并将其更改为一个新的字符串(NewName),正确的语法是什么?我在我的书里找不到。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-05 03:20:25

应该是:

代码语言:javascript
复制
public void changeName(String newName)
{
    name=newName;
}
票数 1
EN

Stack Overflow用户

发布于 2011-04-05 03:21:42

代码语言:javascript
复制
name = newName;

将会工作得很好。字符串是不可变的,因此它不能在以后更改。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5543220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档