//*******************************************************
// 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),正确的语法是什么?我在我的书里找不到。
发布于 2011-04-05 03:20:25
应该是:
public void changeName(String newName)
{
name=newName;
}发布于 2011-04-05 03:21:42
name = newName;将会工作得很好。字符串是不可变的,因此它不能在以后更改。
https://stackoverflow.com/questions/5543220
复制相似问题