[英]Using a method from one class into another class
我在使用最后一種方法時遇到麻煩。 我被告知,我必須在我的bank類的monthFee方法中使用BankAccount.class中的adjust方法,但無法弄清楚。 我嘗試了多種不同的方法,但無法正常工作。 我們需要用月費調整帳戶余額。
Bank.java:33:錯誤:BankAccount類中的方法Adjust無法應用於給定類型; BankAccount.adjust(); ^
必需:發現兩次:無參數原因:實際參數和形式參數列表的長度不同1錯誤Bank.java:33:錯誤:不能取消引用double Fee.adjust(); 1個錯誤
Bank.java:33:錯誤:找不到代碼bank.BankAccount.adjust(); 符號:變量BankAccount位置:類型為BankAccount []的變量銀行1錯誤
BankAccount.class
public class BankAccount {
String owner; // owner of account
int accountNumber; // integer account number
double balance = 0.0; // account balance
double amount; // adjusted amount to balance
String balanceFmt = "%.2f"; // string format for 2 decimal places
public BankAccount(String owner, int accountNumber) { //Constructor for the bank account
this.owner = owner;
this.accountNumber = accountNumber;
}
public double adjust(double amount) { //method to adjust balance
this.balance += amount;
return balance;
}
public String toString() { // method to print out account info
return owner + " owns the account " + accountNumber + " with the balance of $" + String.format(balanceFmt,balance);
}
public double getBalance() { // method to get balance of accounts
return balance;
}
}
Bank.class
public class Bank {
BankAccount bank[];
public Bank() { // constructor for making a 10 account array
bank = new BankAccount[10];
}
public void addAccount(BankAccount accounts) { // add account for giving numbers to accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] == null) {
bank[i] = accounts;
break;
}
}
}
BankAccount getAccount(int i) { //obtain account from BankAccount class
return bank[i];
}
public void printAccounts() { //prints out account statuses
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
System.out.println(bank[i]);
}
}
}
public void monthlyFee(double fee) { //monthly fee for bank accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
} //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
}
}
}
BankTest.class
public class BankTest {
/*
* test - set up a bank and add accounts
*/
public static void main(String[] args) {
// Code to test Bank and BankAccount classes
int errors = 0;
double fee = -2.95;
Assignment assignment = new Assignment();
assignment.homework("Homework 2a");
System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount bankAccount1 = new BankAccount("Joe Mac", 1234);
bankAccount1.adjust(1000.0);
bank1.addAccount(bankAccount1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount bankAccount2 = new BankAccount("Sally Ride", 2345);
bankAccount2.adjust(2000.0);
bank1.addAccount(bankAccount2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount bankAccount3 = new BankAccount("Pat Armstrong", 3456);
bankAccount3.adjust(3000.0);
bank1.addAccount(bankAccount3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");
if (bank1.getAccount(0).getBalance() != 997.05) {
errors += 1;
System.out.println("Balance for account at index 0 does not match $997.05");
}
if (bank1.getAccount(1).getBalance() != 1997.05)
{
errors += 1;
System.out.println("Balance for account at index 1 does not match $1997.05");
}
if (bank1.getAccount(2).getBalance() != 2997.05)
{
errors += 1;
System.out.println("Balance for account at index 2 does not match $2997.05");
}
if (errors == 0)
System.out.println("No errors found!!!");
}
}
非常感謝您的幫助和指導。 謝謝。
簡單:當您檢查測試代碼時,您會發現在調用Adjust ()方法時,它傳遞了一個雙精度值!
您的其他代碼(導致錯誤的代碼)在調用該方法時似乎未傳遞任何重復!
似乎您忘記了傳遞費用參數來調整()方法。 以下代碼工作正常
public void monthlyFee(double fee) { //monthly fee for bank accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
System.out.println(bank[i].adjust(fee));
} //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
}
}
創建銀行1
一個帳戶Joe Mac擁有帳戶1234,余額為$ 1000.00
兩個帳戶Joe Mac擁有帳戶1234,余額為$ 1000.00 Sally Ride擁有帳戶2345,余額為$ 2000.00
三個帳戶Joe Mac擁有帳戶1234,余額為$ 1000.00 Sally Ride擁有帳戶2345,余額為$ 2000.00 Pat Armstrong擁有帳戶3456,余額為$ 3000.00
每月費用997.05 1997.05 2997.05 Joe Mac擁有帳戶1234,余額為$ 997.05 Sally Ride擁有帳戶2345,余額為$ 1997.05 Pat Armstrong擁有帳戶3456,余額為$ 2997.05
錯誤:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.