修改类帐户(附在Blackboard中)提供 •整数类即时变量accountNum作为类号 (不要忘记你需要更改构造函数)。 •一种称为借记的方法,用于从账户中提取资金。确保这件事 借方金额不超过账户余额。如果确实如此,那就是 应保持平衡不变,方法应打印一条消息 表示“借方金额超出账户余额”。 •在方法getBalance()中,添加一行以打印帐号和 帐户余额,然后在测试程序中,您只需要输入, account1.getBalance();而不是printf语句。 已经修改了AccountTest文件以测试您的新Accountclass,所以您 不需要改变它。
这是我的帐户类代码
public class Account
{
private double balance;
public int accountNum;
public int Account;
public Account(double initialBalance, int accountN)
{
if ( initialBalance > 0.0 )
balance = initialBalance;
accountNum=accountN;
}
public void credit( double amount )
{
balance = balance + amount;
}
public void debit(double amount)
{
if (amount>=balance)
System.out.println("Debit amount exceeded account balance.");
else
balance-=amount;
}
public String getBalance()
{
return balance+" "+accountNum;
}
}
以下是AccountTest类的代码
import java.util.Scanner;
public class AccountTest
{
// main method begins execution of Java application
public static void main( String args[] )
{
Account account1 = new Account( 1, 50.00 ); // create Account object
Account account2 = new Account( 2, -7.53 ); // create Account object
// display initial balance of each object
account1.getBalance();
account2.getBalance();
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
double depositAmount; // deposit amount read from user
System.out.print( "Enter deposit amount for account2: " );
depositAmount = input.nextDouble(); // obtain user input
System.out.printf( "\nadding %.2f from account1 balance\n", depositAmount );
account2.credit( depositAmount ); // add amount to account1
// display balances
account1.getBalance();
account2.getBalance();
// create Scanner to obtain input from command window
input = new Scanner( System.in );
double withdrawalAmount; // withdrawal amount read from user
System.out.print( "Enter withdrawal amount for account1: " );
withdrawalAmount = input.nextDouble(); // obtain user input
System.out.printf( "\nsubtracting %.2f from account2 balance\n", withdrawalAmount );
account1.debit( withdrawalAmount ); // subtract amount from account2
// display balances
account1.getBalance();
account2.getBalance();
} // end main
} // end class AccountTest
我无法弄清楚遗漏的是什么,请帮助感谢一帮。
答案 0 :(得分:2)
您只需让您的帐户构造函数按顺序接受double和int。
所以你的主要方法就在
之后Account account1 = new Account( 1, 50.00 );
Account account2 = new Account( 2, -7.53 );
什么时候应该
Account account1 = new Account(50.00, 1);
Account account2 = new Account(-7.53, 2);
java中的所有方法和构造函数仅在您完全按照方法声明中传递值时才有效。通常,每当您看到“找不到符号”错误时,就会出现此问题。
答案 1 :(得分:0)
帐户的构造函数定义为接受double
和int
,而您在此尝试传递int
和double
Account( 1, 50.00 );
答案 2 :(得分:0)
帐户类的构造函数需要一个double后跟一个int,但是你传递一个int后跟一个double。